简体   繁体   中英

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.)

It is not possible that the && operator fails to short-circuit. Were you using & perhaps? If not it means you have made some false assumptions that previous conditions before the last one were false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM