5 qualities of clean code: Your future self will thank you for this
Avoiding brittle code should be among developers’ golden rules but some people prefer to not write clean code and simply get it over with. If the pitfalls of unclean code are not enough to convince developers to tidy up their code, maybe the benefits of clean code will do the job.
There is an old quote (which belongs to either Martin Golding or John F. Woods —depending on whom you ask) which goes like this: “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” Some developers might claim that it’s faster to not write clean code but in the long run there could be many repercussions to choosing the easy way. Robert C. Martin acknowledged in his book Clean Code: A Handbook of Agile Software Craftsmanship that bad code can function too but emphasized that if the code is not clean, it can hurt the organization due to the resources that will eventually be needed to correct poorly written code.
Martin’s book about clean code has a plethora of gems in it that may help developers clean up their code, including the difference between a smart programmer and a professional programmer, namely that the latter “understands that clarity is king. Professionals use their powers for good and write code that others can understand.” The only way to see whether you belong to the first or the second group is to fully know what clean code means.
Writing clean code should be a breeze IF …
According to Martin, “it is not the language that makes programs appear simple. It is the programmer that make the language appear simple.” In short, using workarounds will made the code look strange, so spending more time on finding a clean, good solution is the right thing to do when in doubt.
Simplicity is the ultimate sophistication
This quote describes perfectly the benefit of writing clean code simply because reading your code should be a pleasant activity (when done right). One immediate quality which walks hand in hand with writing clean code is that any developer can easily read the code without trying to detangle it, including yourself months after writing it. Betting on simplicity rather than complexity is something professional programmers understand and apply at all times.
No redundancy, no problem
Don’t repeat yourself! If you’ve heard this piece of advice before and you haven’t put it to good use yet, it should become clear why your code is not clean. When developers apply this principle, they notice that any alteration of any element does not require a modification in other logically unrelated components.
Minimal dependencies = code can be easily maintained and changed
The more dependencies your code has, the harder it is to keep it clean and change it in the future. Nowadays there are many ways to check potential incorrectness in your code’s dependencies and ultimately have minimal dependencies.
Expressiveness makes the need for documentation less important
Code expressiveness means that the names express their intention. Martin encourages developers to say what they mean and mean what they say and warns that “cuteness in code often appears in the form of colloquialisms or slang. For example, don’t use the name whack() to mean kill(). Don’t tell little culture-dependent jokes like eatMyShorts() to mean abort(). Duplication and expressiveness take me a very long way into what I consider clean code, and improving dirty code with just these two things in mind can make a huge difference,” the author of Clean Code: A Handbook of Agile Software Craftsmanship opined.
Avoiding brittle code should be developers’ top priority —otherwise an unclean code will have a snowball effect and cost valuable resources such as time, money and nerves. One thing is sure: if you want to have clean code, make it easy to read!