Saturday, 7 September 2013

Why doesn't this if-statement short circuit?

Why doesn't this if-statement short circuit?

I'm currently fixing a bug in someone else's Java code, but I cannot
explain the bug. The code in question is the following if-statement:
if (locked && DEBUG_ENABLED
&& owner != null
&& (owner.equals(playerName) || subowner.equals(playerName))
&& handleCommand(playerName, message)) {
....
} else {
....
}
In which DEBUG_ENABLED is initialized as private static boolean
DEBUG_ENABLED = false; and handleCommand functions like this:
public boolean handleCommand(String name, String msg) {
if(msg.equals("Command1")) {
....
} else if(msg.equals("Command2")) {
....
} ....
} else { // No matching command
return false;
}
return true;
}
What puzzles me is that even though DEBUG_ENABLED is set to false, the
code still calls and executes the handleCommand function. I always thought
this wasn't supposed to happen due to short circuiting. The if-statement
itself in total is still evaluated as false, since only the code inside
the else-block in the first snippet is executed.
So, how come this if-statement is behaving like this? Is it failing to
short-circuit, or do I misunderstand the principle, or is there something
completely different wrong with this part of code? (Besides the missing
null check for subowner that is, which is done outside of this part.)

No comments:

Post a Comment