bevacqua / js

:art: A JavaScript Quality Guide
https://ponyfoo.com
MIT License
2.93k stars 502 forks source link

Brackets on same line? Not recommended. #6

Closed prettycode closed 10 years ago

prettycode commented 10 years ago

Under Conditionals, you demonstrate the following, below, as "Good":

if (err) { throw err; }

Yes, the practice of using brackets when they're optional is indeed good.

Aside from that, however, I don't believe opening and closing curly brackets should ever be on the same line, as a rule. It encourages poor readability.

When you're visually scanning code, this is much harder to parse:

if (a === b) { doSomething(); }
else if (a === c) { doSomethingElse(); }
else { doTheThing(); }

Than this:

if (a === b) { 
    doSomething();
}
else if (a === c) { 
    doSomethingElse();
}
else {
    doTheThing();
}

Or even this:

if (a === b) { 
    doSomething();
} else if (a === c) { 
    doSomethingElse();
} else {
    doTheThing();
}

Density is not a good thing.

Keeping code terse and line numbers down can sometimes be counter-productive. Same-line opening/closing brackets give developers the illusion of a code block having less logic in it than it actually has.

If I see a method with three lines as I'm scanning, I will likely perceive the complexity to be less than if I see a method that does the exact same thing but is nine lines. The risk is that other developers will come along and stuff more logic into that method with three lines than the one with nine. Code blocks with same-line open/close brackets end up growing larger in complexity, discouraging abstraction and separation of concerns.

White space, on the other hand, is a good thing.

bevacqua commented 10 years ago

Super agreed, maybe you could add this rule to the guide?

I think a one liner if (err) { throw err; } is fine, but definitely not when involving more than one or two statements (such as if (err) { next(err); return; }), and definitely not when involving an else statement.

Let me know if you can add this or I'll get to it at some point

ElLutzo commented 10 years ago

What about a general Bad, Good, Even Better pattern instead of just Bad and Good?

bevacqua commented 10 years ago

Let me know if f4555d14ca4ab4bd8ed7da344310101092965b1a addresses your concerns :)

ElLutzo commented 10 years ago

Perfect. Thank you :-)