Simple ways to improve code readability
Tags: javascript, bestpractices, codeimprovement, beginners
Published: 2019-05-11 14:58:50

---
title: Simple ways to improve code readability
published: true
description: Does anyone actually read this part?
tags: javascript, best-practices, code-improvement, beginners
cover_image: https://images.unsplash.com/photo-1517420704952-d9f39e95b43e
---

**TL;DR: It might be obvious to you, but not to others.**

## Why?
More often than not, I used to think that my code is just for me (_Narrator: It's not_). Whether I'm coding at work or coding for my own side project, at some point someone else will look into the code and try to understand how it works. The last thing you want to happen is them spending too much time doing it.

A little disclaimer: this isn't about rewriting your code and using a totally different programming paradigm (cuz FP the best P), or being overly verbose with variable names and comments. I'll save that for another time. I find that even the smallest changes in the way I write the code could actually help improving the readability. Small enough for a good start.

## In the wild
Let's consider a few code examples.

### #1
```javascript
// When the id is in the list, retrieve by id
if (ids.indexOf(id) !== -1) {
  retrieveItemById(id);
}
```

This code works, obviously. I coded it, merged it, went for lunch, and mostly forgot about it. Then someone else reads this code:

> If the index of the id in the list of ids does not equal to minus one, retrieve the item by id.

That person mutters, _Well, that's one way to put it_.

See, this should be avoidable. That person should never have said that. Anyway, let's take a look at how I should fix it.

It is true that `.indexOf` will return -1 if the value is not in the list, but that isn't what I wanted to say, right? I wanted to _do something when the id is in the list of ids_, instead of _do something when the id is not not in the list of ids_.

It is subtle, but the code in my opinion reads better this way:

```javascript
// When the id is in the list, retrieve by id
if (ids.indexOf(id) >= 0) {
  retrieveItemById(id);
}
```

Heck, I could throw in some [ES6 magic](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) and the readability is now times 3000:
```javascript
// When the id is in the list, retrieve by id
if (ids.includes(id)) {
  retrieveItemById(id);
}
```

But wait, there's more!

### #2
```javascript
// See if any of the results has an error
const isSuccess = results.filter((result) => result.error === true).length === 0;
```

Now, this is similar to the previous one. If the whole ordeal was whether there are no errors in the results, it should be a statement that returns a boolean. Using `.filter` then checking the array length makes it longer to read and understand the code.

```javascript
// See if any of the results has an error
const isSuccess = !results.some((result) => result.error === true)
```

However, putting a negation in front of the statement makes the code less clearer.

> It is a success when _not_ some of the results have an error.


An improved version would be:

```javascript
// See if any of the results has an error
const isSuccess = results.every((result) => result.error !== true)
```

The code now reads:

> It is a success when every result doesn't have an error.

`.some` and `.every` have benefits over `.filter` because it will stop iterating through the array if the statement is true for the former, and false for the latter. So, even from an efficiency standpoint, `.some` and `.every` are better than `.filter` in this context.

Last one, I promise.

### #3
```javascript
// Only ship item that isn't broken and expired
if (!item.broken && !item.expired) {
  shipItem(item);
}
```

Over time, we noticed that we've been shipping items that aren't in stock. Fine....

```javascript
// Only ship item that isn't broken, expired, and stock is not zero (duh)
if (!item.broken && !item.expired && item.stock !== 0) {
  shipItem(item);
}
```

I mean, it looks good, right? Right? _Wrong_.

Again, what I meant isn't exactly _ship the item if the stock isn't not there_, but _ship the item if the stock is there_.

It could be that I was following the pattern of the rest of the logic (`!item.broken`, `!item.expired`) and subconsciously went for another negation, but for this case, it is better to read if it was done without negation.

```javascript
// Only ship item that isn't broken, expired, and stock is there
if (!item.broken && !item.expired && item.stock > 0) {
  shipItem(item);
}
```

## Conclusion
These examples are really simple, easy-to-avoid issues. And of course, there are other different ways to improve code readability. My main point is that we should try to stop having the mindset of 'my code works and I understand it fine'. Coding isn't essentially just to make it work, it's also to educate others on how (and why) it works.

To me, a good code is similar to how people describe a good UX or a good joke: If you have to explain it, it might not be that good. It's other people that defines whether your code is easy to understand, it shouldn't be you. So, whenever possible, second-guess your code and improve them so that others don't have to.

Of course, this doesn't mean that you would have to go down to a level where a biology major should understand your code. I think what makes a good code is also the balance between making it understandable and having to explain everything. And that takes experience.

Thanks for reading (my first article, btw).

---

_Cover image by [Nicolas Thomas](https://medium.com/r/?url=https%3A%2F%2Funsplash.com%2Fphotos%2F3GZi6OpSDcY%3Futm_source%3Dunsplash%26utm_medium%3Dreferral%26utm_content%3DcreditCopyText) on [Unsplash](https://medium.com/r/?url=https%3A%2F%2Funsplash.com%2Fsearch%2Fphotos%2Feye-test%3Futm_source%3Dunsplash%26utm_medium%3Dreferral%26utm_content%3DcreditCopyText)_.