Keeping source code well-formatted and clan is de facto good practice for every developer.
However, many of developers do not keep own code clean and clear. If you have ever worked on a big project or had to support source code of somebody else, you find very often some code chunks that have don't have the best smell.
You may be responsible for creating a few such lines yourself, because “you had deadlines and did not have time for things like code formatting”. A couple of other typical thoughts: "I will make the code clean later...", "this is not an important part of code and nobody will change it in the future...", "why should I spend time on this when I can do real work?".
Familiar phrases, aren't they? I've heard them a lot of times. On one hand, developers know that it is important to keep code clean, and on the other hand, they always have something more important to do instead. Let’s take a closer look at this problem.
When I first started coding, I was typing slow, and I could easily have spent half of the day on typing just a couple of hundreds lines of code. I was focused on improving my coding speed and on my typing skills in general. Coding speed was the most important thing for me during those times. Of course, I was totally right, and nobody wants to spend half of the day typing.
But when I did spend a couple of years improving my coding speed, it turned out that I can provide almost the same couple of hundreds lines of source code. No doubt, that code is more complex, and my productivity is much higher, but the amount of code is around the same.
If you look into this, you can find that averaging over the lifetime of the project, a programmer spends about 10-20% of his time writing code, and most programmers write about 10-12 lines of code per day that goes into the final product, regardless of their skill level. "WTF??? How do I spend the other 80%-90% of the time?" was the only thought came to me reading this sentence for the first time.
Turns out, you spend it on bug fixing, or researching, but not coding. Usually, bug fixing and debugging take 50% of your time (yes, that is half of your work!) and research takes 40% of time. Both debugging and research could potentially take you much less time if the code is clear, because you can understand it at a glance. As soon as I understood that, I’m routinely start my work with someone else’s code, formatting if needed. That helps me understand how it works in detail, and keeps project code on a good level. And I can show my code to any developer at any time and be sure "WTF???" will not be the first thought in her or his head ;)
I hope this article helps you with making a habit to write code well-formatted and clear. Also, here are several tips how to make that easier
- Try to code well from scratch. It's annoying for everybody to get back to code that already works
- properly configure your IDE for project coding standards
- use tools for automatic source code formatting like PHP-CS-Fixer, or check your IDE for this feature
Try hard now and find how clean code will save your time in the near future!