3 ms·
You'd be right, of course, but the original article concedes this point. He's talking about situations like if (a < b) return a return b vs. if (a
by philwelch 17y ago
You'd be right, of course, but the original article concedes this point. He's talking about situations like
if (a < b)
return a
return b
vs.
if (a < b)
return a
else
return b
vs
return ( (a < b) ? a : b)
- neilc 17y agoWhat is the distinction between those two situations? (i.e. those in which the article "concedes the point"?)
- nkurz 17y agoPresuming this is a genuine question: the difference is whether one goes on to do more work after the 'guard' return, or whether one is just choosing which value to return. I think the style guide is being silly, and there is no simple rule that says "always use an else" or "never use an else". I'd see "Avoid unnecessary indentation" as a better rule.
- philwelch 17y agoOne's an early return due to error, one's a conditional return value.