简体   繁体   中英

Why Dead Code Warning?

i have:

  if (Constants.IS_LIVE_MOD == false)
        account = Constants.OM_ACCOUNT;
  else
        account = "abc";

i am getting a dead code warning on 'else'. Why is it so and wat is the solution for it. Please help.

I assume the IS_LIVE_MOD constant is a final variable that is declared as false, if so then the variable is always false and can't be changed, and so the else statement will never be invoked. Therefore it is dead code.

Eg

private static final boolean MY_VAR = false;

if(MY_VAR == false) {
    System.out.println("Always does this");
}
else {
    System.out.println("Dead code");
}

If IS_LIVE_MOD is a compile-time constant with the value false , the compiler knows that the "else" path will never be taken, so gives you a warning. (The Java language specification guarantees that it won't be an error , but it's reasonable to warn you.)

if Constants.IS_LIVE_MOD is a constant and its value is false than the compiler knows that else would never be executed.

You can't have it as a constant.

You're are comparing two constants ( Constants.IS_LIVE_MOD and false ) with each other. As they are constant, the result can be determined at compile time. So the compiler can tell which part will never be executed.

The condition if (Constants.IS_LIVE_MOD == false) always evaluates as true . Therefore else is never reached.

It implies that Constants.IS_LIVE_MOD is always equal to false; so the else clause is never executed; hence the dead code warning.

The meaning of the warning should be clear: the code will not be executed - is dead - since IS_LIVE_MOD is a constant, but here is one solution (workaround):

if (!Constants.IS_LIVE_MOD)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

or

if (Constants.IS_LIVE_MOD)
    account = "abc";
else
    account = Constants.OM_ACCOUNT;

Details JLS 14.21: Unreachable Statements

If you remove the comparison, the compiler recognizes that as a "conditional compilation" and does not show that warning.

Rationale (JLS):

in order to allow the if statement to be used conveniently for "conditional compilation" purposes

If Constants.IS_LIVE_MOD is constant (as its name implies) and is false, then the else clause can never possibly run; this makes it dead code.

Since IS_LIVE_MOD is a constant, the compiler figures out that it will also be false, and that the else part will never be used (until you change your Constants, then it will be the other way around).

Don't worry about it. Ignore or suppress the warning ( @SuppressWarnings("all") on the method, unfortunately has to be all, I think).

If you have "real" dead code, it will make an error.

How is Constants.IS_LIVE_MOD declared? If it's a final field, it's a good chance that the compiler optimizes this expression to be always true.

I'd say the most efficient way of writing this would be like this:

account = Constants.IS_LIVE_MOD ? "abc"
                                : Constants.OM_ACCOUNT;

I guess Constants.IS_LIVE_MOD is a compile time constant with the value set to false. iow it becomes

if (false == false)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

You can just ignore the warning since Java doesn't seem to support conditional compilation Java conditional compilation: how to prevent code chunks from being compiled?

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