hyperoslo / android-playbook

MIT License
6 stars 2 forks source link

Brackets and ifs #15

Closed ealmdahl closed 8 years ago

ealmdahl commented 9 years ago

Standardize how we do bracketing in general and ifs in particular.

//oneliners
if (foo) doSomething();
else doSomethingElse();

//no brackets
if (foo)
    doSomething();
else
    doSomethingElse();

//brackets
if (foo) {
    doSomething();
}
else {
    doSomethingElse();
}
sindrenm commented 9 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();
}
Damian-Lopata commented 9 years ago

I agree with @sindrenm I like to have brackets ...ALWAYS .. even if there is a single line of code in it. Easier to read.

sindrenm commented 9 years ago

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:

ealmdahl commented 9 years ago

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?

changxiangzhong commented 9 years ago

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();
}
sindrenm commented 9 years ago

Totally agree with the else and else ifs on the same line as the ending brackets!

ealmdahl commented 9 years ago

Totally disagree on else and else ifs 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.

sindrenm commented 9 years ago

Hmm, how about try..catch..finaly statements, though, @ealmdahl? Would you write them on separate lines as well, like this?

try {
    // ...
}
catch {
    // ...
}
finally {
    // ...
} 
jeantuffier commented 9 years ago

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 {
    // ...
}
ealmdahl commented 9 years ago

@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. :)

sindrenm commented 9 years ago

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:

Damian-Lopata commented 8 years ago

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 {
    // ...
}
sindrenm commented 8 years ago

Great!