I always try to avoid nested statements.
They lead me into putting code into a long curly braces. It becomes hard to follow the code when the amount of code with in each condition becomes larger.
if(condtion) {
} else if(condition2) {
} else {
}
So if I have code like this:
void doSomething(){
if(condtion) {
return;
} else if(condition2) {
return;
} else {
return;
}
}
I always change it to this form (I avoided the else
as there is a return
statement from each condition):
void doSomething() {
if(condtion) {
return;
}
if(condition2) {
return;
}
return;
}
But someone told me mine is a bit hard to read, which one is better?