Javascript: Braces with single-line if blocks

Created on 16 Dec 2016  路  1Comment  路  Source: airbnb/javascript

In point 16.1 you state the two block possibilities when it comes to in-line if blocks. These are:

if (test) return false;

if (test) {
  return false;
}

Is there any preference to use one or the other?

Personally, I use the first one only at the top of a function, when I have to verify trivial preconditions. (i.e. if (!name) return 'Name is required';.

question

Most helpful comment

This style guide allows for both, however I personally strongly prefer the latter, even in the case of early returns, because omitting braces where braces are optional is the cause of many bugs.

I agree that the one-line version makes the most sense when only used for early returns.

>All comments

This style guide allows for both, however I personally strongly prefer the latter, even in the case of early returns, because omitting braces where braces are optional is the cause of many bugs.

I agree that the one-line version makes the most sense when only used for early returns.

Was this page helpful?
0 / 5 - 0 ratings