Why Writing Explicit Code Matters
May 18th, 2023Explicit code, code that clearly shows its intent, it’s good. we all agree. And yet most of us don’t write explicit code, we write code with all kinds of implicit meanings that we try to covey via online comments or documents. Why is that?
When we are writing code we have all of its meaning in our heads and in most cases, any code we write will look like expresses its intent explicitly, so we look at it and are happy with it. That’s our mistake.
The mistake is that we don’t go back and revisit our code, questioning our decisions about naming and general structure and trying to find a way of writing the code that makes comments obsolete. We have code that works but that is not good code.
Explicit code is important because it can be understood by someone who doesn’t have any context about it, the code itself provides the context. Explicit code is code that can evolve. Implicit code is code that will rot.
Explicit code is code that can evolve. Implicit code is code that will rot.
Best practices to follow and improve
Peer reviews from someone with fresh eyes always help, but in my experience, not all developers will pay attention to this matter in code reviews, and also, I think you should not depend on others to make your code good.
If you’re writing your own code pay attention to the comments you write. Each comment is a sign that your code is not explicit enough to be understood without a comment
If you are reading somebody else’s code and you need to go back over and over the same piece of code and you feel the naming doesn’t help following the code, there’s another sign of code without explicit intent. Improve it, don’t be afraid of changing it, even if other people think it’s irrelevant or that you’re just being a perfectionist. People in the future will thank you.
Try to review your own code after one or more days of writing it. You’ll be surprised at how much context you have forgotten and how little you get from the code. Use that as a tool to identify where you can improve it.
Conclusion
At the end of the day, good or bad code is subjective and each of us has different preferences. But if you write code with explicit intent there’s a good chance that people will consider it good code.