9
During a change in a source code, I came across the following situation, developed by another developer:
if ( booleano1 || booleano2 || booleano3 )
{
if( booleano1 )
{
//faz algo
}
if( booleano2 )
{
//faz algo
}
if( booleano3 )
{
//faz algo
}
}
Clearly, the developer’s intention was to verify if one of the conditions was true before performing the 3 if
separately. The only justification I could find to have been implemented in this way would be a (insignificant?) performance gain.
So my question is: is there any improvement in performance when using various conditions in the same if
in relation to implementing several if
separately?
That looks like microtimization neura huh
– Wallace Maxters
It is, in my conception, even if there was a gain, it would be insignificant even for being a primitive Java operation, but this implementation left me with the doubt if there really is a difference.
– dnr1
@Wallacemaxters Depends on what the boolean expression does. If the developer repeated the logic in each IF the code can be beeeem slower and no need. I’ve seen it a lot in the name of saving a variable.
– utluiz
It seems to me a case of a skeptical developer. After all, these languages today are not very reliable. Will the variable change from an hour to another without explanation? To ensure even more I would do so:
if (booleano1 && booleano1)
. Always check twice if you want to be sure. Better safe than sorry.– utluiz
@utluiz sério ? (booleano1 && booleano1) ? mds kkk
– Gabriel Rodrigues
@Gabrielrodrigues To be better just doing
booleano == true && booleano == true
. Prevented programmer counts for two. D– utluiz
@utluiz usa === not to do uhauha type conversion
– Gabriel Rodrigues
what the world would be without the irony hahahahahaha
– dnr1