Clean Code: Psychology behind writing a good code

Bhuvan Gandhi
4 min readApr 17, 2022

--

As a programmer, you may think what is the connection between psychology and code as computers only understand the code? Well, the connection is between the human minds, one who is writing the code and the one who is reading it :)

Do you know what is difficult than writing a code? It’s reading someone else’s code. We all have experienced this in our journey. Because while writing the code, we are reading our thoughts and converting into a code. And while reading a code, our mind is looking for the connection between the lines of code and their purpose of existence. So, it may take much time than expected to understand the written code. But how do we know what we understood is correct or not!

A new perspective

Let’s see this from another point of view. As you have written the piece of code, can I say that your thoughts are the blueprint (algorithm) on which this piece of code is built. So, now whenever someone reads that code, they are actually looking at the structure of the blueprint rather than the blueprint itself. So, will they be able convert your code to the exact blueprint from which it was written? More often no, right? It’s called mental mapping — which we should avoid. Let’s understand this from an example. Here is the basic blueprint (algorithm) on which we are going to build the structure (code):

Code:

Now forget about the blueprint. Only from the code, can you derive the blueprint? Difficult, right? Let’s try this another way.

Now try to convert your code to the blueprint — easy-peasy, isn’t it? So, what makes this significant difference? It’s the psychology while writing the code — The way we give names to the variables, functions, classes etc., the way we arrange them, the way we delegate responsibilities among the functions and classes etc. This will save one’s a lot of time while debugging, reading and understanding the code. Even this is true for the programmer who wrote that code also. Because we are not made to remember the code we wrote for a whole life. We will most likely to forget what and why we wrote that piece of code in just 2–3 days. So, instead of doing mental mapping later, it’s better to write a clean code now.

I will write only clean code from now.

What is wrong with the comments?

No doubt in that comments can convey the same thing which code says, even in the better way. But issue with the comments is that in-case if we forgot to update comments after updating the code, then it may take to disaster when someone is trusting the comments instead of code. It will create ambiguity while understanding the code. This is what a great programmer Mr. Robert C. Martin teaches us about writing the comments:

The proper use of comments is to compensate for our failure to express ourself in code.

And it is absolutely correct. Because comments are there because your code is not the self-explanatory one, but it should be. Your code should yell to reader that why it is being written over there and what purpose does it serve.

This is also not true for all the cases. There will be sometimes when you have to write comments in your code to justify your step, but that is okay. Because you are not justifying your code, you are justifying your logic behind the code.

Isn’t it dependent on the programming language?

Well, it’s not true for any modern programming languages. Because they are come with the lot of concepts to give you flexibility in writing code. Some things may differ, but the foundation principle will remain the same for all. Mr. Robert puts this in this way,

It is not a language that makes program appear simple. It is the programmer that makes the language appear simple.

Conclusion

This article was intended to motivate you to write clean code. The clean code approach is much-much deeper than what I have explained. To know more about this, I would like to encourage you to read a book, “Clean Code” by Robert C. Martin. I have learnt a lot from this book. I hope you will also.

In case, you are still not motivated for writing a clean code, I strongly recommend you to watch this video: https://youtu.be/-1CuAiKdBQs. You will surely fill necessity of writing the clean code :)

Additional Resources

Regarding Clean Code: https://cleancoders.com
Summary of Clean Code book: https://github.com/ryanmcdermott/clean-code-javascript (Note: Reading summary != Reading book)

--

--