Unity: The Importance of Modular Code Part 1
Why do we want to limit repeated code?
When programmers here someone says the word “dry”, we aren’t looking for a towel. For t he uninitiated, “DRY” is perhaps the most quoted acronym in software engineering — and for good reason. If you find yourself repeating the same functionality, you’re probably doing it wrong. There is almost certainly a more efficient option that will save you time, space, or even both.
The ability to optimize your game code so that it doesn’t take up any more space than need be and doesn’t suck up all the client’s resources is one of the most important considerations during the production process. Freeing up the space and memory so that you can prioritize the player’s experience is paramount. One of the primary enhancements you can introduce to accomplish this is modular code.
What do I mean by modular code? In general, modular code extracts common (read: repeated) code and turns it into a single function (sometimes called a module) that can then account for the different uses of the functionality. That’s a lot of words. I think a more detailed tutorial is called for.
For our endeavor, I’m going to take inspiration from a very familiar game in hopes that it will help simplify the concept. We are going to create a modular function that controls the behavior of the power items (or powerups, if you prefer) for the classic NES masterpiece Super Mario Bros (abbreviated as SMB). If you need a trip back in time, MarioWiki has you covered.
I do have one caveat to mention. I am only concerned with the items from SMB — not the overall gameplay. For example, in the context of SMB, the items would normally pop up from the bricks and/or golden boxes throughout the game. In our example, we are not concerned with how the items spawn. We only need to know that each item has a unique power and we will be controlling that power with our function.
Like a horrible cliffhanger, I’m going to leave it there. I won’t make you wait until next season for the continuation though. See ya tomorrow!