javascript - What is the downside of using one-line if statements? -


i see code-bases using bracketed if-statements in cases including cases in 1 statement needs executed after if.

are there clear benefits using bracketed if-statements or has become standard on time?

what general consensus on this, big mistake allow one-line ifs through our linter?

largely comes down developer preference, although many reasons given using curly braces on every control block.

first, makes control block more readable. consider:

    if (some_condition) runsomefunction();         runsomeotherfunction(); 

since indentation not respected in curly brace languages, work, reduces readability (only runsomefunction() happen in control block). compare to:

if (some_condition) {     runsomefunction(); } runsomeotherfunction(); 

second, when need add control block (which invariably happens more not), adding curly's can frustrating or forgotten leading issues above.

still, largely come down preference , can find exceptions (like if (some_condition) runsomefunction(); more readable first example above while still accomplishing same goal in more concise format retains readability).


Comments