When you first start programming, the problem you're trying to solve is "how do I write code that'll do ______?". Initially you're happy just to get something working, who cares what the code looks like. But when you try to develop a complete game this indifference can become a problem. As your code gets messier and messier, the project becomes harder to work on - you're more likely to write code that has bugs and they become harder to track down and fix. Eventually it'll get to the point where you're wasting a lot of time, don't feel productive, and are more likely to stop working on the project.
As a more experienced programmer, instead of asking "how do I do ______?", you should start asking "what's the best way to do ______?". In this post we'll look at different ways to write a simple function to see how certain ways are better than others.
It's a good habit to write code in such a way that you can add new features later. You never know what you might want to add to your game, so it's good to make everything generic and flexible so you can easily implement any new idea that pops into your head.
But sometimes it's better to do the exact opposite.
It can take a lot more time to write good code. Cutting corners can save you a lot of time. The key is determining what corners you can cut - the right shortcuts save time, the wrong shortcuts will end up costing you more time later on.
Decide what features your game won't have.
Suppose you're making a somewhat typical RPG where players have items and slots to equip them in. You can spend weeks making a completely generic system that supports a flexible number of slots with complex relationships between items and slots they can be equipped in based on the player's character. For example:
A normal, human-like character has the equipment slots you'd expect, but a two-headed ogre has two helmet slots, two necklace slots, and four earring slots. To a human character a bracelet goes on the wrist slot and a belt goes on the waist slot, but to a giant bracelets are treated as rings (placed on the finger slot) and belts are treated as necklaces (placed on the neck slot). A shapeshifting character can transition between normal form and wolf form - in wolf form they get four bracelet slots but lose some other slots.
This sounds really cool but it also sounds really time consuming to develop. If you can say with certainty that your game won't have these features, you don't need to waste the time making it. If you're only going to have three item slots - helmet, armor, and weapon - there's nothing wrong with doing this:
If that's all you need, that's all you should write! Some people would look at that and think it's terrible because you can't easily add the features I mentioned earlier. What would really be terrible is spending more than five seconds writing this code if you know it's never going to be more complex than this.
Decide what needs to be done now, what can be done later, and what doesn't need to ever be done.
Suppose you're working on the character creation system for an RPG. When creating a character the player will get to type in their name. There may be many concerns you'd have about players selecting their own names:
1. You don't want duplicate names.
2. You don't want bad words in names.
3. You don't want names with characters that are difficult to type (©, §, ¥, etc.).
4. You want names to follow roleplaying rules (ex: so no characters are named "LukeSkywalker3142")
If there are still other parts of the game you need to develop, you don't want to waste a lot of time on this. It's important to do things in such a way that you can expand them later but not consume a lot of time right now. Things can easily get out of hand:
Suppose you want to make names unique across all servers in case you later let players use the same characters across multiple servers. Suppose you want to have a central database of bad words that all servers refer to - this way you can add to the list at any time. Suppose you want to have character names be approved by someone (ex: the game master) before they can be used.
These things can take a lot of time to code but you don't need to implement them all right now. Some of those things you may never need to implement. Right now, all you really needs is a proc like this:
if(length(n) < 2 || length(n) > 50) return 0
As long as you call this in your character creation process you can later expand this check to include more features. This is all you need right now and you can get back to implementing more important things. You might eventually realize that some of the other features (like a central database of bad words) aren't needed. Any time spent expanding this feature right now when you should be focusing on more important things is a waste. Any time spent adding features here that will never be needed is a complete waste.
Some parts of your game will be simple, some will be complex. There's nothing to gain by writing complex code to do a simple task, there's just a lot of time to waste. Identify when you can keep things simple and use that to your advantage.