Mohammad D.

How do you write good code?

Mohammad Dohadwala4 min read

Markus Spiske

Photo by Markus Spiske on Unsplash

Writing readable code seems like a lot of trouble for many programmers. This doesn't just apply to beginners, but unfortunately also to those who have years of experience in the field. Why is that? How can we make our written code more readable and accessible?

Recently I spoke with a few of my programmer friends with various levels of experience, and I am afraid that the readability of a code is still treated as something of very little importance. However, nothing could be more wrong!

Programmers spend most of their time reading, not writing code. Adding a short snippet from Stack Overflow often requires reading dozens of lines of code. This is one of many reasons why readability should always come first. Unreadable code greatly increases the time it takes to write anything new.

Optimization is not a process

If you are optimizing your code and not taking measurements to confirm performance gains, the only thing you can be sure of is that you have made your code less readable.

Martin Fowler

Martin is a highly skilled speaker and author in Software Development. His view on optimization is, you are making your code worse if you are blindly optimizing it without actually measuring the performance gain.

When someone talks about performance and uses the phrase "I believe", a warning light should come on for everyone.

"I believe I can compare these values up faster if I sort them first". How much faster? How many milliseconds? How many percent? How does this affect the perceived speed of the app and the user experience? These are extremely important questions, and without answering them all, your "optimization" is a waste of time.

If we go back 15 years, programing was very different back then. We had very low-level languages, where you had only 8KB of RAM and every byte mattered. As a result, you couldn't write clean code.

This isn't true for modern languages. Compilers today are very advanced tools. Almost works of art. The result of years of research and work by mathematicians and computer scientists. Do you really think you can optimize your code more than the compiler? I can assure you that 99.9% of the time you can't. This is especially true in Just in Time (JIT) compiled languages, where it intelligently optimizes the pieces of code that are run more often.

Therefore, just spend time writing code that is clean and readable. And unless you are an expert, forget about optimization. Code optimization in most cases is better left to the machines.

Which of the following javascript code will run faster?

Example 1

function getCenter() {
const x = width / 2
const y = height / 2
return Vector2(x, y)

Example 2

const getCenter = () => Vector2(w / 2, h / 2)

If you said example 1, you answered incorrectly. If you said example 2, you are also incorrect.

Typically, both of these snippets will be compiled to the same executable code. Therefore, neither of them are faster than the other. But the first example is much more readable. This is why you should always put readability before optimization.

But, who cares?

I created my startup and here everything changes like in a kaleidoscope. We do not have time to worry about the readability of the code, because in two months, half of the application will be plowed up anyway.

It is absurd to say that writing unreadable code is faster than writing human-readable code. A complete contradiction. After all, one of the main advantages of writing clean code is that it speeds up development.

Let's consider that by naming a variable x instead of playerPosition we gain about 1 second. If we multiply this by 10,000 lines of code, we get about 3 hours. What a difference, we will be the first one in the market, yay!

Okay, but what are we missing here? First of all, hours or even days for introducing new programmers to the team. Days and weeks of implementing new functions in a maze of incomprehensible code. Hundreds of malfunctioning errors that shouldn't have happened. Is this okay in a fast-growing start-up? I don't think so.


From this long post, let's remember three things:

  1. Optimization is not a process.
  2. Put code readability first.
  3. Seriously, just write readable code and don't worry about the rest.

Understanding and applying these two principles has enormous added value for any professional. Thanks to them, you have a chance to write code that is better, more understandable, and extensible. It will also be easier for you to introduce new people to the team, and once you get into some habits, you will save a lot of time reading your and others' code.

Liked it? Share it!