Writing better code
Day 315 / 366
I spent a lot of time today cleaning up the code for one of my projects. It’s a bittersweet feeling. On the one hand, it’s something I enjoy doing, it’s like organising your desk. It feels good to rewrite badly rewritten code and get rid of useless lines.
On the other hand, depending on how big the codebase is and what stage the project is at, it could be really scary as well. Even a single line change can lead to multiple things breaking. And that is why people always put off doing that. I remember at one of my previous companies we used to have scheduled breaks from regular work where everyone would just work on code cleanup and optimization tasks.
And it’s days like these that make me appreciate the importance of writing better code. Anyone can write code that is correct. We are taught how to do that in most colleges. But clean code is something different. How you would name your variables and methods, and how you would structure your code into blocks, all these are things that you best learn from experience.
How could I have avoided this code rewrite? Well, there are several basic reasons. Firstly, I should be focusing on code quality from Day 1. It’s easy to be careless when the project is starting, since small mistakes don’t have a big impact. And hence we compromise on code quality.
Also, you should write tests for your code. The biggest fear I have while rewriting code is not knowing if it will break anything or not. A good test suite can help with that.
And lastly, when working in teams, every code push should be peer-reviewed. The reviewers have a responsibility to uphold the code quality standards as well.