The importance of Clean Code

Lalit Mehra
CodeX
Published in
3 min readMar 25, 2022

--

I often review code; not just within my team, but as a part of cross-team initiative as well. The exercise is both refreshing and exhausting. At times I find a totally new and innovative approach to solve the problem at hand, but that isn’t always the case. Most of the time the code is plain and simple and often lacks basic sanity.

Developers often miss the point of writing clean code and the importance of it. The code is written in a manner as if no one will read the code ever again, as if it is written to be obsolete in a moment. I have seen some very large method definitions, one line methods that just make a call to a utility class and return, overloaded methods with the entire content duplicated and much more to be expressed here.

Writing code and writing clean code are two very different things. It is one thing to write it for the compiler to understand and totally different for a human to understand it.

While it is important that the code works correctly, it is also important that it is readable because if it is not then it is asking for trouble. A working code isn’t always the most readable and easily understandable. On an average it takes more time to read and understand someone else’s code that to write it on your own.

We must ensure that our code is written in a manner that at least we can understand it without difficulty, when we read or work on it again.

Code that is not clean is not efficient, while it might look so at first. There are many small but significant changes that can be introduced to ensure clean and readable code. Some of them are listed below.

1. Method name should describe its intent

2. Methods should always perform a single task

3. No more lengthy methods, with many things crushed into one

4. Use expressive variable names

5. Pass object(s) as argument(s), if possible

6. Write comments to express complex logic

7. Use interfaces to avoid coupling

These are just a few ways to write clean and efficient code, there is much more to learn in this context and introduce to the code as a general practice. Considering that we start with a few of these and make it a habit we can transform our code to a great extent in no time.

--

--

Lalit Mehra
CodeX
Writer for

Your friendly neighborhood software-engineer. I write about software development, design and architecture, and life in general.