I used to believe that the most important quality of code is its extensibility. The code had to be prepared for future changes so I would code lots of things not necessary now. But after realizing that I never was right upfront guessing what the future changes will be, and after having quite a few maintenance and handover nightmares, a few a years ago I decided that I will take the opposite approach to what good code is. Ever since I did that, I am every day more convinced that the key characteristic of good code is simplicity. Simple code is easy to read and easy to change. And because it is simple, it is less prone to have bugs (the more complex the algorithm you are creating the more changes to make a mistake).
Whenever you are coding, I recommend you to check every time if your code is still simple, and remember: “there are no hard problems, only hard solutions”
The seven main characteristics of simple code are:
1.- Easy to read.
Simple code doesn’t need additional documentation, or it only needs minimal documentation to be understood
2.- Easy to use.
Whoever is using your code will find it intuitive to use your objects.
3. Easy to change.
Simplicity means that there is no duplicated logic. A change should match a single place in your code.
4. Doesn’t use any non-necessary third party, tool or technology.
Through my experience I have observed how some developers just love to use technologies, tools and frameworks just for the sake of making the project more “cool”. Every time you use them, you are adding extra complexity, and extra complexity means that is more difficult to understand, to maintain and is more prone to failures.
5. It looks simple.
If it doesn’t look simple, it is not simple! You know your code is simple if when you finish you are astonished at how simple the final solution is and you ask yourself how it was possible that it took you so long.
It only does what is necessary, and nothing else. I think most seasoned developers would agree that trying to anticipate the problems your code will have to face in the future is impossible.
7. Is straight forward.
It doesn’t have unnecessary indirections; the most important operations only require a straight method call.
How can we develop simple code?
The key of producing simple code is continuous refactoring, and the only way to do this is continuous testing.
You are going to need refactoring because every time you add a new line to your algorithm, you are probably making it more complex. So every now and then you should refactor to get it back to a simple stage.
You need many tests because you are doing so much refactoring, and if you don’t have a safety net you are going to introduce bugs into your system.