Well, some well-known Java experts argue that one should always strife to minimize the scope of things and that's also my opinion.
My teachers at university always tormented me that I must write stuff like this as posted by somebody before:
String result = null;
doLotOfStuff();
if (condition) {
result = foo;
} else {
result = bar;
}
return result;
The variable result has now a pretty big scope. In addition with a lot of nesting this tends to get very unreadable if you do a little bit more here, say you have another for-loop.
I think this is much better:
doLotOfStuff();
return condition ? foo : bar;
This is straight to the point. I see immediately what's going on, no checking of curly braces.
I think it's very good to
- Minimize scope
- Minimize indention (that is avoid curly braces)
- Minimize method length
The "Unnecessary else statement" warning helps with that.