Did you ever have to go back to a fragment of code that you wrote a month or year ago? How did it feel? Was it easy or did you have to figure out how it worked from scratch? If you need more than just one look, there is a good chance that you are doing something wrong. And if you scratch your head and think: “What the heck was I thinking?”, you have definitely done it wrong. But what has gone wrong? Most probably the code works fine and at some point you knew it inside out. Why can’t you remember it now? Maybe your code wasn’t written clearly enough and in accordance with best coding practices? Here are a few tips on how to write easily readable code not just for yourself but also for other developers.
Example of using coding standards
Consider the following method in C#:
foreach(DateTime kc ins)
At first glance you have no idea what it actually does or what it can be used for. But after short refactoring we can get:
What has actually happened to the code? Several modifications have been made to increase its readability:
The name of the method has been changed from one that really doesn’t say anything to one that is a little bit more descriptive.
The way of naming the variables was changed:
“kc” was changed to arrivalTime,
“s” was changed to arrivalTimes,
“d” was changed to stringBuilder,
It also much easier to understand what each variable is responsible for and how it was used.
Parentheses have been standardized to one format.
Tabs have been added to increase readability, spacing, and nesting in braces.
The “?:” operator has been used to reduce the length of the code by 4 lines.
The StringBuilder class was added to avoid string concatenation (“string” + “string”). Although some may argue that creating the StringBuilder instance will slow down the method due to its memory allocation, I would like to remind everyone that string concatenation creates a lot of allocation for Garbage Collector to clean up. It is considered that ~5 string concasts are equal to creating instances of StringBuilder, so if a list consists of 5 or more elements the performance of this method will actually increase. And for larger collections this method will work several times faster.
Example of good coding practices
Let us go through another example. Consider User class (just for demonstration purposes):
// write to Data Base
And a method that is responsible for updating user status that has to check if all properties are in correct state:
The main modifications that have been made to this code to increase its readability and maintainability:
A static method has been created to check if the user status can be updated. This makes the UpdateStatus method way simpler to understand at first glance. Also, the CanUpdateStatus method logic can now be reused in other parts of the system as it is public and static.
All the “if” statements have been reversed to reduce nesting. Now almost all of parentheses are gone and it is much easier to read or debug through the code. Another advantage of this convention is its scalability. Imagine that the User class has now three more properties that need to be checked before updating status – 3 more “if” statement nestings would be necessary. Now you can only add three more lines in the CanUpdatedStatus method.
Strings that represent messages were removed from bodies of the methods and were put in constant variables. This also helps with maintaining the code because regardless of the number of usages of the code there is only one place where you have to change the content of the message if needed. To be honest though, all the texts should be transferred to an external resources project.
But how can you know what conventions you should use when writing a code? Where to look for all good practices? And where to find all (or most) of this knowledge?
I have participated in the GET .NET conference recently. A number of speakers talked about countless benefits of writing clean and understandable code. An argument that convinced me most was a quote from Udi Dahan who said: “Write your code as if the next programmer that will work with it is psychopath serial killer that knows where you live”. Now go and refactor your code and let me know how it changed your coding experience.