Hey there, Gamers and Game Makers!
In this weeks blog, I'm going to be going trough five basic yet fundamental tips for writing better code. So, let's get right into it!
#1 Be Mindful of Variables
Keep in mind that every time you create a variable, you a reserving space in memory for that variable. Any time you leave unused variables in your code, you are wasting memory. And while this may not seem like a big deal given how powerful computers have become, it's still bad practice and to be avoided. This also raises the point about needing to think more carefully about your variable declarations. Look at your variables and ask yourself, do I really need that variable there? Can I achieve the same result in my code without the need to store something in that variable.
#2 Comment Your Code
While this may seem like an obvious one, even I still need to remind myself from time to time to comment my code. It's important from the obvious point of helping others you work with or someone who will have to manage your code at a later point to understand what your code is doing.
Hell, even if you're the only one who will ever be looking at the code, it's important for you to remember what your code is doing. There's been plenty of times when I came back to old code I've wrote that I forgot to comment and the amount of time wasted in trying to understand what I did and why I did it. So, comment your code.Even if it's just for you.
#3 Understand Your Code
There's going to be times when you are faced with a problem with your code and you may need to seek out a solution online. You may get it working as you wanted but now you don't fully understand why it works. This becomes a short term solution to a long term problem. You won't always be able to do this. You may run into a similar problem later down the line but now, the code you used before doesn't work.
You need to learn what your code is doing. If you don't understand a part of your code, take it and break that section down into the most basic example possible. Play around with it and understand what it's doing and more importantly why it does it that way. If you can understand what a line of code is actually doing, then you can better understand how to apply that to future problems.
#4 Use Meaningful Names
When naming your variables, try to make them as meaningful as possible. Having abstract or nonsensical variables is asking for trouble. As your code grows, you're going to have more variables and if you start to get confused as to what a particular variable is used for, then you're going to run into a lot of errors. Make your names clear to the purpose the variable serve.
#5 Don't Reinvent The Wheel
Programming has been around a while now. So, needless to say there are already good solutions to common coding problems you will encounter. Use them! There's is no need to create extra work for yourself by trying to think of another way of solving said problem. Unless you can come up with a better way, use the tried and tested method. The only thing you need to be sure of is that you understand the method and the logic behind it.
Until next time!