The first draft of this article was about the code-style. I wanted to show you why it is important and why we should care about it. But the longer I thought, the stronger conviction I gained, that the code-style – whatever it means – is only a way to achieve a much more important goal.
I’m a big advocate of well-quality code. Besides good architecture, I pay attention to style – consistent spacing and indentation, coherent naming convention and other rules that make the code visually better. But how to convince someone, who never took care of style before, that is it a good thing?
Why should you take care of code?
It’s not obvious but the code you produce is not for the computer. Machines don’t need a beautiful code. They don’t even need your fancy file structure or well-designed architecture. A compiler translates your work to the machine code that doesn’t resemble your handsome code at all.
So why should you care? Because you write for people, not machines. Machines can run the program like a flash, whereas people need to read the code line by line to imagine what it does and what would the outcome be.
The good code makes this process easy.
By focus on the people, you switch the mindset from “Does it work?” to “Is it easy to understand?”. The computer understands the crappiest code in the world as long as it’s valid. People need more time to connect the dots and see dependencies in the code. You may help them by reducing the cognitive load by using proper abstractions, expressive naming, and consistent style.
Even if you think that it doesn’t apply to you, you should care. If you conduct a project alone in a company, someone else may join you because of higher demands. If you leave your job, another developer takes over your work. How smooth this process will be?
Instead of asking “Does it work?”, ask “Is it easy to understand?” and change, until the answer is “yes”.
Still in doubt? A personal project you’re working on would be something bigger one day or would be stuck in your hard drive. It’s your decision. Even so, it’s something that you write for your future self. You may come back to this code in the future, so it should be easy to understand and make a pleasure to work with.
Code style is subjective, but it doesn’t matter. It’s everything about our customs and habits. Each team, company, and community has its perspective. Because you write for people, you will know how to do it right.
Take care of the people, who will work with your code. Help them understand what the code does. Treat it like the common good. You will see, how its quality improves in time. People will thank you someday.