Consider these to options
if(successful)
{
if(condition)
{
//do something
}
if(condition)
{
//do something
}
...
}
or
if(successful)&&(condition)
{
//do something
}
if(successful)&&(condition)
{
//do something
}
...
Imagine there 100 if statements.
Is there any difference in efficiency?
Thanks in advance.
That all depends how costly it is to evaluate the
successfulexpression.You should also note that the two versions are not semantically equivalent as the evaluation of the if-expression might have side-effects1.
If you are actually facing performance issues then measure, don’t guess. Measuring will be the only way to see what the performance really is.
1To explain a question from the comments, here is a simple example where you would get different behavior:
The method
CreateProcesshas the side-effect of starting a new process and indicates the successful creation by returningtrue:This is quite different from the following: