Roberto Tonino

1 small tip to improve your code readability

Posted on:

Code readability is important.

When you find yourself in situations like this one:

function doSomething() {
  // some code...
  let needToDoALotOfThings = /* test */

  if (needToDoALotOfThings) {
    /*

       A good amount of code

    */
  }
}

you can refactor it in this way:

function doSomething() {
  // some code...
  let needToDoALotOfThings = /* test */

  if (!needToDoALotOfThings) return

  /*

     A good amount of code

  */

}

or, even better:

function doSomething() {
  // some code...
  let needToDoALotOfThings = /* test */

  if (!needToDoALotOfThings) throw new Error(/* error message */)

  /*

     A good amount of code

  */

}

The difference is slight but significant. By using this approach, you will have (at least) 2 advantages:

  1. 1 less indentation level, that is always good;
  2. Your condition is shrinked in 1 line of code, making the code easier to read in future reviews.

You obviously can’t use this approach everywhere, it depends on the situation (as always), but it’s a small correction that can save a bit of brain cells to the person that will read that snippet of code in the future.