Shorter code is better?
Posted on November 27, 2014 • 2 minutes • 250 words
Which one do you prefer: shorter code or more legible code? As for me, legibility of the code always come first. Working in a team will make one realize the importance of code legibility sooner or later. Your code is not for you alone to read. It should be friendly and easy for whoever has to maintain your code as well. It’s not easy to write elegant code but it’s much easier to write legible code.
- Always give a good name to your variables. It’s not random naming was talked as one of the hardest problems in computer science.
“There are only two hard things in Computer Science: cache invalidation and naming things.”
- Phil Karlton
-
Comments: comment is not a bad thing. But lots of comment definitely is. It might be a signal that the code is not legible enough that it needs that much lines of comment for readers to be able to understand. Also, it’s not a good thing to comment code and leave it there. It will create confusion for those who maintains it.
-
Uncouple your code. Uncoupling increases legibility and writing test unit a lot easier.
-
Write everything as if you were to write an API, as it should be intuitive and easy to understand. Your code should be easy to make changes when needed. It should not break a whole system when does.
“Writing specifications is like writing a novel. Writing code is like writing poetry.”
Your code can talk, so tell a story.