3 ms·
I use both styles in the same code: if (simple_condition) { } else { } for (long and stuffed; expressions; here) { } if ((compl
by kazinator 11y ago
I use both styles in the same code:
if (simple_condition) {
} else {
}
for (long and stuffed;
expressions;
here)
{
}
if ((complicated ||
multi_line)
&& condition)
{
}
void function()
{
}
In an if, all the "else if" and "else" clauses have to follow the same style. If any is braced, they all have to be.
By the way, is grandparent kidding about Go enforcing this?
The only language I can think of which enforces curly brace placement is Awk. (And only in its pattern-action pairs):
BEGIN {
function()
{
if (foo)
{
}
}
}
/regex/ { # can't move this curly to next line
action
}
- kazinator 11y agoRATIONALE: why the two styles? Why if (simple_condition) { code; } but: if ((complicated || multi_line) && condition) { code; } ? Simple: it's because in both cases, the "code" follows a previous line from whose first non-whitespace character it indents by the indentation level: if (... [INDENT]code { [INDENT]code This is just wacky: while (blah blah blah) { code } "code" isn't "indenting off of" anything. It is not related to the indentation of "blah" in any way, but appears to be "deindenting" from it! So, rather than dogmatically doing one thing or another, it's better to consider all the alternatives, their relative advantages and disadvantages, and integrate them into an elegant whole.