Closed ealmdahl closed 8 years ago
Essentially, I find that if I'm writing guard clauses, I prefer putting them on a single line. For instance:
if (holder == null) return;
// ...
Whenever I write statements (for, if, while, …) on two lines or more, I always prefer brackets. So not this:
if (that)
doThis();
Rather this:
if (that) {
doThis();
}
I agree with @sindrenm I like to have brackets ...ALWAYS .. even if there is a single line of code in it. Easier to read.
How about one-liners, though, @Damian-Lopata? With brackets, I suppose we could roll with something like this:
if (holder == null) { return; }
Although, for oneliners, I actually prefer having no brackets. :smiley:
I like brackets, too. I'm not very fond of oneliners in the first place, but IF I use them, I prefer them without the brackets. @tuff @changxiangzhong, your thoughts?
Like Damian, I prefer bracket ALWAYS. @sindrenm I think the else should start with the previous end bracket, like the following
if (foo) {
doSomething();
} else {
doSomethingElse();
}
Totally agree with the else
and else if
s on the same line as the ending brackets!
Totally disagree on else
and else if
s on the same line. I see each if and else as its own block, and you wouldn't start an if directly after after a }
?. But if I am alone with this point of view, I'll let myself be assimilated and conform.
Hmm, how about try..catch..finaly
statements, though, @ealmdahl? Would you write them on separate lines as well, like this?
try {
// ...
}
catch {
// ...
}
finally {
// ...
}
I agree to the no bracket oneliner:
if (holder == null) return;
and for multi condition:
if (foo) {
doSomething();
} else {
doSomethingElse();
}
same goes for the try/catch:
try {
// ...
} catch {
// ...
}
@sindrenm: yes, I would, actually. I think it makes it clearer where the blocks of the statements start and end. But again, it might just be me. :)
You agree to no brackets, or brackets, @jeantuffier? You said no brackets, but your example showed brackets, so I got confused. :stuck_out_tongue:
And I do see your point, @ealmdahl. I just think the } else {
is more concise without sacrificing readability. :smiley:
Seems like we have agreed on something here, well most of us. I will add this to the playbook in this form for brackets:
//oneliners no brackets
if (foo) doSomething();
//other cases have brackets
if (foo) {
doSomething();
} else {
doSomethingElse();
}
if (foo) {
doSomething();
} else if(...) {
doSomethingElse();
}
try {
// ...
} catch {
// ...
} finally {
// ...
}
Great!
Standardize how we do bracketing in general and ifs in particular.