Write code for humans, not for machines

Vishwesh Jainkuniya
4 min readAug 30, 2019

Here is a quote from Martin Fowler

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Because the compiler doesn’t care whether your code is cleanly written or unreadable mess — as long as the syntax is correct, the code will compile and it will run. If you are writing code for machines, then

why aren’t you writing code directly in 0s ans 1s? 0️⃣ 0️⃣ 1️⃣ 0️⃣ 1️⃣

It’s the programming language which is always helping you in readable code.

Easily readable code is easy to maintain. From business point of view the shorter it takes to understand the code for a new programmer, the less money is required to bring the new person up to speed. Thus, cleaner code has more worth.

When it comes to optimising code, most engineers will be frighten to touch messy code. He will think,

I might break something in refactoring/optimising this code, so let’s not do it.

On the other hand, it the code is easily readable, then anyone can take it up for refactoring and further optimising it.

From Structure and Interpretation of Computer Programs by Abelson and Sussman

Programs should be written for people to read, and only incidentally for machines to execute.

What usually happens in big companies and organisations? 🤔
I would like relate this with a poem from high school: The Brook by Lord Tennyson

For men may come and men may go,

But I go on for ever.

This is what exactly the code itself thinks. New programers will come and go, but I will always be there. So previous programmer should left me a state from where the new programer can easily pick me up.

One more important thing:

There will be always be some situations where cleaner code is taking 5% more time to execute, but corresponding complex code is taking long time to get understood by new developer. In such scenarios, if your team is growing fast its better to go with cleaner code. It will help a lot in new dev on-boarding and increase team velocity significantly. But if this piece of code is only touched by only one-two dev, then reducing executing time should be top most priority.

So it depends from case to case, you can not always stick to one.

Now, let’s see some very basics but effective ways to write clean readable code:

  • Indentation/Line length

#1

$my_email->set_from('test@email.com')->add_to('programming@gmail.com')->set_subject('Methods Chained')->set_body('Some long message')->send();

#2

$my_email
->set_from('test@email.com')
->add_to('programming@gmail.com')
->set_subject('Methods Chained')
->set_body('Some long message')
->send();

Just after having a quick look on #2, anyone can understand what it is suppose to do, but not with #1.

Also, if anyone intends to read the code from a terminal window, such as Vim users, it is a good idea to to limit the line length to around 80/100 characters.

Do you ever wondered why newspaper articles have narrow columns of text?

Because our eyes are more comfortable when reading tall and narrow columns of text.

SAME goes with code as well 🙌

  • DRY Principle

It stands for Don’t Repeat Yourself. Also known as DIE: Duplication is Evil.

If you are repeating same code again and again, then first of all it becomes very hard to maintain it. As in future, if there is slight change in the requirement then you need to do changes at various places and it is very likely that you will miss out some places.

So it significantly effects code readability.

  • Avoids comments as much as you can

Code should be as simpler as it can be (of-course not violating anything). Please don’t write obvious comments like:

// if country code is US
if ($country_code == 'US') {

This just adds noise to your code, and unnecessary increases file length 🤷‍♂.

Martin Fowler says:

When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous.

Think twice, when you THINK of writing a comment next time.

This are the some ways in which you can write clean code. There are other ways as well. I have highlighted only this, because I think they are very easy to take care while writing code, and affects the readability a lot.

Thanks for reading . I hope you understood, why it important to write clean readable code, because we write it for humans. Your peers should able to understand it . WE DON”T WRITE CODE FOR MACHINES. And its not hard to write clean code. Just we need to take care of small small things. 🙌

Hope you enjoyed reading it. Next blog post coming soon 🙈

--

--

Vishwesh Jainkuniya

Building @gojektech, Open source contributor @Zulip, previously @goibibo. Creator of Tafy, College Plus, Udhaar, @botlatexx https://jainkuniya.github.io/