I have this code :
if (something.equals(something1)) {
myObj.setB开发者_Go百科oolean(true);
} else {
myObj.setBoolean(false);
}
Is there any reason to write above code instead of just :
myObj.setBoolean(something.equals(something1));
From a technical perspective: no. From a human perspective: maybe; it could make the code more readable and easier to follow.
I am not very knowledgeable when it comes to what kinds of optimizations that is performed by various compilers, but in theory it could be that the if-else
block is effectively translated into the shorter version, since they are logically the same.
One difference is that it's easy to set breakpoints on the former.
However, the former contains a redundancy that is displeasing and carries a tiny risk of divergence.
Most debuggers support breakpoints conditional on value. This alternative supports breaking on true or false, without the redundancy.
boolean isEqual = something.equals( something1);
myObj.setBoolean( isEqual );
精彩评论