In general, I believe it's easier to read a single if
instead of one or more of them.
Here are some scenarios for comparing the two options.
Many conditions
Thinking about cases with if
s, you'll see that this:
if ( A > X AND A > Y AND A > Z)
instrucao aqui....
ENDIF
It's simpler to understand that:
if ( A > X )
if ( A > Y)
if ( A > Z)
instrucao aqui....
ENDIF
ENDIF
ENDIF
Extract Method
Using a single if
, you can simplify the meaning of conditions by creating a single method, which is not possible with a if
within another if
.
As an example:
if ( A > X AND A > Y)
instrucao aqui....
ENDIF
You could have a isAMaiorQueYeX(A, X, Y)
method, giving an explanatory name for the condition:
if ( isAMaiorQueYeX(A, X, Y))
instrucao aqui....
ENDIF
For more complex cases, this type of artifice becomes more and more useful.
Debugging code and errors
A small advantage that exists using a single if
is debugging problems. If a "NullPointer" error or something similar occurs, the line pointed to in the error will direct in the problematic condition. Now, using multiple conditions on the same line, you're not sure what condition caused the problem, and you'll have to debug the application to understand.
And speaking of debugging, it is also made easy with many if
s. Because debugging is done line by line, it's a little easier to see what's happening in the code.