Brackets on same line? Not recommended.
prettycode opened this issue · comments
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.
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
What about a general Bad, Good, Even Better pattern instead of just Bad and Good?
Let me know if f4555d1 addresses your concerns :)
Perfect. Thank you :-)