;

Nabeel Sulieman

How to Write Clean Code

2018-10-11

TL;DR

Some simple practices to help you write reasonably clean code, even when you’re in a hurry:

  • Split your code into small and simple functions
  • Avoid too much indentation
  • Use good variable and function names
  • Don’t repeat yourself
  • Do the easy optimizations
  • Practice your trade

Background

Writing clean and elegant code is not easy, especially when you’re under pressure to deliver on a deadline. Sometimes you have to cut corners and whip something up that just works.

However, I often see code that just makes no sense at all. Things where no matter what kind of deadline you’re under, you really shouldn’t be doing. Keeping these simple things in mind will help you develop cleaner code even when you’re in a hurry.

Minimize Function Size

Some people say that a function should never be more than 10 or 20 lines. I don’t know what the optimal number is, but let’s at least agree that a function should fit on a single screen.

Minimize Indentation

Highly indented code is hard to read. You really should never have more than two levels of nesting. Instead of nesting, separate code out to new functions.

Keep it DRY

DRY stands for Don’t Repeat Yourself. Never copy-paste large chunks of code. It’s a recipe for disaster later on. Instead,create a function (or class) and re-use it in the different places that need it.

If you have large chunks of duplicate code, making changes in the future becomes very troublesome. You end up having to make the same change multiple times in different places in the code.

Perform Basic Optimization

It’s true that you probably shouldn’t spend time over-optimizing your code. That is often a waste of time. However, even when quickly hacking code together, it’s not hard to write code that performs reasonably well. Let’s look at a few examples:

const boolean isFlag = checkSomething()

foreach(...) {
   if (isFlag) {...}
   else {...}
}

We see that the variable constant is being set outside of the loop. If you know that constant isn’t going to change for the duration of the loop, then checking its value on every iteration is a waste. Here’s another example:

if(var) {...}
else {...}

......

if(var) {...}
else {...}

In most cases, code like this can and should be combined into one if-statement. It should be possible to write code where if-statement conditions and switch statement arguments are never duplicated.

Avoid ELSE when Possible

Some people argue that you should never use else in your code. At the very least, I would say avoid it when it’s unnecessary. For example:

if (something) { return; }
else { /* do something */ }

There’s no need for the else in this case. The following is exactly identical:

if (something) { return; }
// do something

Avoid Long IF Conditions

You know you’ve gone too far when the condition inside your if-statement is 40 lines long. For example:

if (
   something ||
   something_else ||
   something_else_2 ||
   ......

) {/* do something */}

Just take that if condition and put it in a function:

if (checkIfTrue(...)){/* do something */}

Practice

You know what they say: practice makes perfect. Look at other people’s code. Revisit old code that you wrote and think about how it could be made better. Practice coding on sites like LeetCode and HackerRank and check out other people’s solutions.

Conclusion

No excuses, start writing better code today!