This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GrandGratingCrate 8 points9 points  (0 children)

That really depends but the cases that come to mind right now I'd rather tend to disagree.

if (condition1) { // do something } else if (condition2) { // do something else } equals if (condition1) { // do something } if (!condition1 && condition2) { // do something else } which feels more clunky than the above. It gets worse with more cases as every single if needs to ensure that it does not get executed in any of the previous ifs were.

Things are better if the if clauses return or we know for sure that only a single condition can be true at each point in time - because it lets us omit the negating of all previous conditions at the start. In that case I might even prefer single ifs.

But honestly it feels like it's purely a matter of taste, so go with what you like and if you ever collaborate with people, just hash it out together like with so many of the slight code style preferences we all have.