When coding it is easy to end up in a situation where the code is difficult to read by other developers or hard to maintain and make changes too. In this article I will provide you with some tips that will hopefully allow you think differently when writing code which will help you make your code more readable and maintainable.
Here are some tips you can use when trying to name a variable, method, class or anything else.
- When choosing variable names try to have logical names instead abbreviations.
Instead of doing something like this:
Try something like this:
- Avoid very long names for variables. As a rule if it needs to be anything longer than 30 characters then it usually means it is too long.
- Make sure you have consistent naming conventions. For example if you are using pascal casing continue using that through the code base.
Variable Placement and Magic Numbers
When creating variables a lot of new developer tend to declare all the variables they will use at the top of the class or method. However what then happens is that you end up with a list of variables that you end up using a few lines further down. It makes it hard to read and it does not make sense as why would you declare something if you are not going to use it straight away. It could have potential performance implications on your application depending on what you are initialising.
Instead of this:
Magic numbers can be described as random integers used for bits of logic. For example you could have an IF statement that checks if the status of something equals 1. However what does 1 mean? As the person writing the code it could be very clear to you that 1 means “Draft” however, for someone reading the code it could be confusing.
Instead of this:
Try to create an external class or some sort of dictionary depending on the language you are using that holds the value and string representation of that value:
A common thing that many programmers do is nest if conditions in their code. For example if they have two things they want to check like is this variable empty and if it is then do another if inside to check something else as well like string length is grater than 0 for example. This can bloat your code, make it hard to refactor and add additional logic later on.
For example try not to do this:
However, if you try and put if conditions that needs to be met before anything in the method works at the top before anything else, it will improve readability and performance of your method:
By looking at the example above there are two ways you can tackle the above example. On line 25 you can see that we combined both if statements into one. For the very simple example given in the article it would probably make more sense to combine them. However, in a more real world example I would suggest putting if statements that are checking if certain requirements are met at the top. If they are not met then return out of the method. This will help your method’s performance and readability.
Comments in your code are on of the things that can really make your source very bulky for no reason. So many programmers add comments to their code that describe what the line bellow does. There is no need for that!
It causes two issues when you do that. One it could double the size of your method if you have a comment above each line of code. Two, comments can become out of date if someone updates the code but not the comment. This can cause many issues for someone trying to fix a bug and it means that you are essentially maintaining two sets of code when making changes.
Always assume that the person reading the code can read code and does not need explanation like “Setting variable to 1” because in the line bellow they can see “var temp = 1;”.
For example do not do this:
Instead only use comments on very complex business logic scenarios where comments might make sense to give user context as to why something was done that way. But never use comments to explain the code. If you need comments to explain the code then you should probably refactor the method.
I hope the above tips have given you something to think about and do in your everyday coding. These are very simple things that we can all do to make our code more readable and maintainable. The examples in the article were inspired by C# however all the tips are transferable to other programming languages.