Welcome back to my backend series, inasmuch as we would be writing codes and working on several projects ,it is very necessary to highlight things which we would regard as very important when writing these codes, we would definitely want to write quality codes.
When i started off writing codes, i could write a whole lot of lines of code, over 500 lines of codes, and guess what?, these codes were trash, none of them could be pushed to production, and with feedbacks i got from senior and mid-level developers i could fix them right and write quality codes.
What is quality code?
In the most simple term, quality code is code that defies all odds, code that has minimal bugs, code that can easily be read and understood from the first insight, it can be said to be code that follows the rule of the programming language which it is being written in.
I’ve had the pleasure of working on a lot of projects over the years. I’ll be the first to admit that they haven’t always turned out the greatest, but they have always helped me learn how to become better. Today, I am confident in my ability to write quality code. Without further ado, the following is a list of tips and tricks I’ve compiled that have helped me to really improve the quality of my own code.
1. Use a Linter
So many problems can be solved by using a code linter. For anyone who’s unfamiliar with linters, a code linter reads your code and outputs errors and warnings if your code is not compliant with the specifications and standards of a language. These warnings may seem insignificant at the time, but, like the lint in your house, they add up over time.
2. Comment Your Code
There are usually two types of developer when it comes to commenting code: the type that comments everything and the type that doesn’t comment anything. The truth is, there are benefits and drawbacks to both sides. One the one hand, leaving too many comments in your code can just make it more cluttered and harder to read. On the other hand, having no comments whatsoever can leave a future developer in a state of turmoil.
3. Writing Legible Code
One of the most important parts of writing quality code is writing legible code. After you write some code, take a short break for however long it takes you to clear it out of your head (this break could be hours, days, or weeks). When you return to the code, if you can’t understand it by reading through it once, it should be refactored in some way, shape, or form.
The following tricks have helped me to write more legible code. These might seem like they don’t help all that much, but making your code as legible as possible can make a world of difference to someone who has to maintain it in the future.
Use Well-Formed Names
Use well-named functions, classes, and variables. A name should tell you the microscopic story of how a thing is used and what it represents. It shouldn’t be a single letter, like “x,” and it shouldn’t be a 70-character-long sentence full of underscores.
Suffix Variables and Functions With Units
For variables and functions dealing with units, append the unit to the end of them. For example, if I have variable called “width” that represents the width of an element in pixels, it would be better named “width_px.” Using this naming scheme ensures the person using the variable cannot overlook the units it is in. A simple thing like using a variable’s unit in its name can really help to mitigate critical bugs in production.
Stick to the Standards
Use the language standards for variables, functions, strings, comments, etc. It’s not necessarily bad to do things differently, but it can throw people off—especially when they’re trying to remember what a function/variable was named, going to search for it, and struggling to remember the right nomenclature.
If you’re using Python, name your classes with Title case and your functions/variables in sn
4. Testing Your Code
In the beginning of this article, I mentioned that in order for code to be considered high quality, you should be able to prove it functions properly. Well, the proof is in the pudding, and the pudding is this: you need to provide a sufficient amount of testing to prove to an outsider that your code works the way you intended it to.
5. Code Review
Code review is the ultimate test of how well your code can be understood by others. Take advantage of it whenever you can. Having someone else be able to read and understand your code in front of you is the ultimate test of whether your program is going to be maintainable.
Furthermore, our judgment can often become clouded after we’ve been working on the same project for months on end. It becomes necessary for someone other than yourself to review your code in order to get an unbiased opinion.