Member-only story
Want To Improve Code Readability? — Here Are 5 Rules
Code readability is a feature of your application (even if your users don’t see it)
I have five rules to improve code readability. I’ve learned them in all kinds of projects, teams, and organizations. I hope that you can pick some things from this article to improve your code readability.
TL;DR
To all of you searching for quick tips without reading everything, read the TL;DR version below:
- Reuse what will be used more than once.
- Readability and maintainability over a generic solution.
- Make modules, classes, or components as small as possible.
- Have rules and guidelines for your code.
- Code like you’re in a team — even a one-person team.
1. Reuse What Will Be Used More Than Once
Most developers know what D.R.Y. means (Don’t Repeat Yourself). D.R.Y. can help you prevent code duplication.
Why write a function over and over again? You should write it once and reuse it in multiple places. If you need to change that code, you only have to look in one place instead of copy-pasting a bug fix in multiple places.
But be aware that you will introduce complexity with D.R.Y. because, in the end, things will be reused more and more.
The importance of writing tests when reusing parts of your code will be very clear when you start changing that code.
2. Readability and Maintainability Over a Generic Solution
Reusability, readability, and maintainabilities are each other’s friends and enemies simultaneously. When you start applying D.R.Y. to your code, you introduce complexity. When you introduce complexity, the readability grade can go down.
So don’t start with a generic solution when building features. Start simple! The first time can’t be perfect.
You can reuse parts of the application through iterations but still guard the readability and maintainability.