There is no magic bullet for clean readable code but these are tips that usually overwhelmingly increase code readability.
1) Declaration close to usage
Instead of declaring a variable and use it way below and have random code in between, have the declaration and the usage as close as possible.
Instead of having code inside an if and maybe an else, do a quick one liner guard check in the beginning of a function and have the majority of the code unnested outside of brackets in the top scope of the function.
3) Reduce the number of parameters in a function
A function with a long list of parameters usually mean that it is highly coupled and doing a lot of things. 1, 2 or sometimes 3 parameters is okay but more usually mean that the function or the parameters need refactoring.
4) Naming variables
A lot of time naming variables to exactly describe what they do goes a long way into making the code more readable Also breaking logic and encapsulating them into intermediate variables improves readability too Do not be afraid in having long variables names.
Did I miss anything? Feel free to add them in the comments.
I will keep adding to this list on this post.
If you want to stay up to date, subscribe below to the newsletter.
Software Engineering from the Frontlines Course on Maven
If you liked this article, I will be teaching a “Software Engineering from the Frontlines” course on Maven where I will teach hard-learned lessons I acquired developing large-scale products at companies such as Uber, Airbnb, and Microsoft.
Thanks for reading Software Engineering Tidbits! Subscribe for free to receive new posts and stay up to date.