In this article, we will go through the 4 major coding tips that I have learned over the time through experience.
These coding tips are actually quite obvious but not always applied. I would like to thank my seniors for constantly pin pointing those areas which required optimization. And with time I have realized that these little tips can improve your code quality significantly.
Without further adieu, let’s start with our tip number one ->
The perks of adding comments to your code
This is one of the most important tip and shortly we shall see why.
You can quote me when I say “No one wants to deal with a piece of code which is not properly commented.“
Because it is difficult to understand a piece of code especially when it’s being used to handle multiple functionalities. Moreover, if commenting done right, it can break down a single large piece of code into multiple meaningful segments. And so revisiting that code to understand it or make changes will be a lot more easy and time savvy.
A proper comment explaining the working of your code also helps your code reviewer in review process.
I cannot stress more when I say, you should always take some time to provide proper commenting on your code before delivering it.
Reduce your code to few lines by creating reusable functions (DRY – Do not Repeat yourself)
If you are writing same piece of code again and again then it is a self-evident sign that you should refactor it into a reusable function.
Keep your functions short and make sure your functions do not perform more than one task. And if you ever find yourself with more than one reason to change your code then you are already breaching the Single Responsibility principle.
So to explain in brief, always keep your code short and follow the single responsibility principle. And you will find that your code has become more modular and easy to handle.
Increase readability using consistent naming conventions
You should always follow a consistent naming convention. It helps in reducing the efforts of reviewer. And makes it easier for literally everyone to understand your code better. Even yourself !!
The names of the variables should be self-explanatory so one can easily guess what that variable is used for. And of course same goes for the names of functions.
In other words, good naming conventions can make your code very easy to understand.
Avoid hard Coding values in your code
Our next tip must seem quite obvious to everyone who has some coding experience especially in professional capacity. So let’s catch up to the main course right away :-
So firstly, as evident by the title, you should always try to avoid hard coding values into your code. Hard coding values might make the initial development quick and easy but at later stages it becomes impossible to reuse or even refactor.
A quick solution to that would be to put the values in external sources like config or resources file. Another solution would be to declare a constant and use that constant in your code.
So take it from me when I say, “Having all your hard coded values in one place will definitely make your life a hell lot of easier.”
To conclude, always adhere to the coding tips we have just discussed in this blog. Following these guidelines will help you grow as a coder as you move along in life. Even in personal projects that you might pursue as a hobby, these tips will help you write code that is modular, adaptable, extendable and easy to understand.
Believe me when I say “Follow these tips and it will be a cakewalk to ever revisit as well as refactor your code.”