Brackets and Single Line Conditionals

Alan Levicki
Katana Stab Studios Blog
2 min readMay 10, 2017

I believe using single line conditionals, such as

if (!usernameIsValid()) return false;

as opposed to

if (!usernameIsValid()) {
return false;
}

is one of those things that people have strong personal preferences about. I come down firmly on the side of always using the brackets and the extra line. I feel that it is easier to read and am not bothered by the extra lines.

I recently had a discussion with another developer where he tried to convince me that keeping it on a single line was better. Let’s examine some of his arguments for the single line conditional.

  • It saves space
    Technically this is true, but this is not a valid concern. Space in source control systems is not at a premium. A lot of code gets compiled, so the space savings is done at that step, and if you are using Javascript chances are the code is minified.
  • It is more readable
    This is probably down to personal preference. I would argue that the brackets and multiple lines are more readable because a block is being created, so it should have indicators and when browsing code it may be easy to skip over a single line conditional.

Arguments for using multiple lines

  • Consistency
    Using brackets and multiple lines for a single line conditional is consistent with how they are used for a multi-line conditionals. This means it is easier to read because it is expected
  • Readability
    This may come down to personal preference, but I think it is easier to read the multiple line version in the midst of a larger block of code. I will grant that there may be times when the opposite may be true (although it may be that there is a better way to write that code), eg
if (form.name.isInvalid()) return false;
if (form.address.isInvalid()) return false;
if (form.zip.isInvalid()) return false;
if (form.state.isInvalid()) return false;
if (form.city.isInvalid()) return false;

Again, I believe the choice here is mostly personal preference. I do not think that using the lines creates any large issues in a codebase. But due to the consistency and readability issues I prefer to never use single line conditionals.

--

--