Clean code and its benefits
Have you ever experienced a huge decrease in productivity in your projects over the years? Have you ever lost days trying to figure out what a code does? Maybe the problem isn’t you but the people who wrote that code.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand”. — Martin Fowler, 2008.
I have yet to find a better definition of a good programmer. Making code work once is easy, the challenge is to make code that will survive over the years.
“Clean code always looks like it was written by someone who cares.” — Michael Feathers
If you just want to finish your tasks, get it all done and move on quickly, I have to say that you are causing big problems for yourself and your coworkers in the future. If you don’t do it right from the beginning, even you will not be able to understand that code even one month from now.
And trust me, that will happen when someone asks you to make changes or maintain that code after a while. When you start caring about whether the code you are writing is comprehensible to others, that’s when you can start calling yourself a professional.
Robert C. Martin, known as Uncle Bob, wrote a book called Clean Code in which he teaches us how to make code that you and anyone else can understand.
He tells us that even the best teams start getting slower over time and he dives deep into this problem to understand why. He found that the reason is very simple: your team can’t work properly if the code isn’t clean. Your developers lose a lot of time trying to understand the code and they are left with a very short amount of time to really be able to write any code themselves.
It doesn’t matter if you hire more people to your project, it only gets slower because the new developers are going to waste everyone’s time trying to understand the project. When they finally start to write some code, they won’t do it right because they have been taught by the ones who made the mess in the first place.
The good news is that it doesn’t have to be this way. You can create lots of new features and still be able to move fast.
“The only way to go fast is to go well” — Robert C. Martin
Uncle Bob also tells us that our code is not going to be clean at first. The human brain works in a messy way: it goes from a high level code to a low level in just one line. He says that we need to make it work and then we have to clean our code. It’s okay if you copy and paste some code and your system is working fine, but it’s not ok if you leave it that way. You have to do it right before you consider that task to be done.
“Half of your job is to make code that works, half is to make code that other people can maintain” — Robert C. Martin
It makes sense when you realize that when you’re developing, you spend much more time on reading someone’s code than writing your own. One day, Uncle Bob analyzed his history of activities on his own code editor and he found out that there was a 10:1 proportion of reading to coding.
Over the years, Uncle Bob has been teaching us how to do Clean Code by establishing some rules. In the next article we are going to talk about these rules.
Martin, Robert C. Clean Code: A Handbook of Agile Software Management. Prentice Hall, 2008.