My name is Dmitriy Savchenko. I’m a Team Leader at AllStars-IT Ukraine. In this article I want to talk about clean coding. One of the reasons I decided to write this article is the fact I noticed doing code reviews for some developers, couple of them had 8 and more years of experience. The fact is while writing the code people don’t actually tend (care) to write clean code!
So what is clean code? To answer this question I always suggest to read the same named book by Robert C. Martin (I am big fan of it and strongly recommend to each developer I work with).
Clean code – it’s a way of writing code. Basically you can write code in many ways and it will work (maybe ). Until… Somebody else will have to work with it, or you will need to extend it after some period of time. Messy code brings poor understanding and increases the risks of the bugs to appear. Besides, last but not the least – makes code untestable. It is known fact that companies spend huge amounts of resources in working hours because of poorly written code. So why not to make it well right from the start?
Let me give you an example of some code to be improved:
And same example in a clean code way:
In result both code samples should give identical outcome. But pay attention on how much of your time it takes to understand first code snippet and second one? Now let’s imagine that whole solution is written in a first way, time to understand it increases correspondingly.
Some people might say “Why not to add comments to the code?”. This is another approach which can bring you to confusion and potentially increase time to understand the code. Why? Because you can never trust comments, but you can always trust code that is written.
Let’s imagine common situation: we have some code, covered with comments and then somebody who has fixed bug in it and changes behaviour (in most cases he will not change comments). And next person who will work with this code will be totally confused due to comments and code implementation discrepancy. The only way I would suggest to add code comments is to do it in the following way: https://
So if you don’t wanna end up with comments like this
//When I wrote this, only God and I understood what I was doing
//Now, God only knows
Make it clean code! Try to group your code into methods. Besides potential re-usability it give you better readability and ease to make it more testable. I am not saying about making methods public. I am saying about extracting methods to separate class and the cover it tests the way you prefer.
So, I guess that’s it for now. Really hope this note was useful and will help you write and read your team code as “well-written prose”.
Team Leader at AllStars-IT Ukraine