My IDE (IntelliJ IDEA) is telling me that I have the option to remove the braces on this if statement:
if (objectIsOfTypeFoo) {
if (objectOfTypeFooIsShared) {
// do something with Object of type Foo knowing that it's shared...
} else {
// do something with Object of type Foo knowing that it's not shared...
}
} else if (objectIsOfTypeBar) {
...
}
To become:
if (objectIsOfTypeFoo) if (objectOfTypeFooIsShared) {
// do something with Object of type Foo knowing that it's shared...
} else {
// do something with Object of type Foo knowing that it's not shared...
} else if (objectIsOfTypeBar) {
...
}
I understand how this makes sense, and it's tempting to lose the indent, but my concern is that readability may suffer. The latter looks cleaner, but is the space saved worth the potential confusion?
I assume the performance difference between the two is trivial, if at all.
And as a follow up question: Is there a limit to how many 'if (condition)'s you can fit in a single line, or rather at what point does it become too many?
I'm voting for the way you already have.
I don't even use this:
if(foo)
return bar;
I like this instead:
if(foo){
return bar;
}
"programs must be written for people to read, and only incidentally for machines to execute"