JavaScript

Simplify your functions that return Booleans

For a program to do anything interesting, it needs logical conditions to decide which bits of code to execute. Too many conditions can be…

Fotis Adamakis
Fotis Adamakis
Senior Software Engineer / Technical Writer
2 min read
January 4, 2023

[Code Review Tips]

Simplify your functions that return Booleans

For a program to do anything interesting, it needs logical conditions to decide which bits of code to execute. Too many conditions can be cumbersome and make the code hard to resonate. You might have encountered the terms spaghetti code or big ball of mud to describe this situation.

The solution is to split the codebase into multiple smaller and individual chunks that are responsible for something small or, even better, only one responsibility (Single responsibility principle). Small functions are easier to understand, debug and test.

But there is a small anti-pattern that I have found myself commenting about in code reviews often. It is demonstrated in the following snippet.

function isValid(data) {
  if (data.isValid) {
    return true;
  } else {
    return false;
  }
}

Abstracting the validation logic to a function is a good idea, but the implementation could be much simpler.

function isValid(data) {
  return data.isValid;
}

The second snipper is much smaller and easier to understand. The first example is a simple example of [code smell](https://en.wikipedia.org/wiki/Code_smell) that eventually piles up in the technical dept that makes everything hard to work and reason about.

Short-circuiting

Usually, logical operators are used to combine multiple boolean conditions. But they can do more than that.

When checking a logical condition like a || b && c the check will happen from left to right, and when the condition is truthy, the result of the current evaluation will be returned.

We can use this knowledge to simplify our return statements.

const data = {
  valid: true,
  available: true,
};

function isValid() {
  if (data.available) {
    if (data.valid) {
      return true;
    }
  }
  return false;
}

Can be simplified to:

const data = {
  valid: true,
  available: false,
};

function isValid() {
  return data.available && data.valid;
}

Same with the logical OR

let data;

function isValid(data) {
  if (!data) {
    return "Empty";
  }
  return data;
}

Can be written like this:

let data;

function isValid(data) {
  return data || "Empty";
}

Conclusion

Less code is not always better. You should aim for readable code, and the second snippers are much easier to understand, in my opinion. I will leave you with one of my favourite quotes.

Anyone can write code that a computer can understand. Good programmers write code that humans can understand -Martin Fowler

Fotis Adamakis

Fotis Adamakis

Senior Software Engineer / Technical Writer

Experienced software engineer writing about front end architecture, accessibility, system design, and developer productivity. Lessons from building and maintaining large-scale frontend applications, with a focus on practical patterns that make codebases easier to understand, scale, and evolve.

Barcelona, Spain