Code Quality Cheat Sheet
How To Avoid Bugs In Your Code?
Here we are going to try and summarise how you can effectively avoid bugs in your code and share some of the most reliable code quality best practices. You wouldn’t want to find a moth in your code would you? 🪲
Other than risking finding a bug in your code, why should you care about code quality at all? Well, let us explain to you why we think it is important to bear these healthy habits in mind.
Firstly, you obviously want to make sure that your code, or some pieces of it, won’t crash in the process. While reviewing code, you want to do it from a continuous improvement perspective, you must make sure that your code is only upgrading and not regressing. Finally, this process guarantees that your code is easily readable and understandable by any person other than you, the author.
Now that we’ve convinced you that code quality is not something to treat lightly, let’s get to it! Here are 10 tips to write beautiful code:
Follow the single responsibility principle
Quite simple, each and every one of your functions should be written in order to serve only one purpose. In short, one function = one use.
Chose explicit variable names
If you don’t, other people might not be able to correctly read your work — they cannot read your mind, at least not yet! Seriously though, maybe even you wouldn’t be able to read it after a few days 😅
Implement coding conventions
Whether you are working on your personal side project or for your company, you should be following coding conventions to have clearer and easier to read code. These might be more or less flexible depending on your company or situation.
Reuse your code
The more you write, the more you risk making mistakes. If you are refactoring your code, even if it wasn’t perfect, you will avoid having to write something which you already coded all over again, and will contribute towards improving the overall quality of your code.
Rely on existing libraries
Well documented and tested libraries will keep you from writing mistakes and save you some time, don’t stop yourself from at least looking into it.
Ask your peers
Having someone else review your code is crucial if you want all the odds stacked in your favour to successfully avoid software bugs.
Don’t rely on code comments
It’s a good thing to add comments on your code to specify what a complicated block is meant to do, but you shouldn’t rely on them to understand the whole piece. If you do, that probably means that your code is not straightforward enough and that others might also struggle with it. It’s all about keeping things simple!
Keep it simple
Try to break up your code commits as much as possible. Shorter blocks will be easier to read and understand, it will also be simpler for the testing stage.
Test your code
We will never say it enough but testing is a major part of your coding journey. Testing at different stages will allow you to detect bugs easily and will therefore save you some time later.
If you are still reluctant to unit test, we have written a piece that might convince you to adopt this good practice and will increase your code quality. We would also be more than happy to introduce you to our unicorn which will take care of it for you, go try it out!
Take your time
Nobody’s perfect and we can all be rushed into doing something when under pressure. Try to take a minute and ask yourself if this is really the right way to do it, the time that you don’t take now will probably have to be caught up later because of some bugs found in the code. Think twice, think smart 😉