2
    if (myCondition1 && myCondition2 && myCondition3)
    {
    ...
    }

I wrote this code and run successfully. but I got warning about part of (...). The warning is "Dead code". It is just interesting to me. Do u have any idea? thank u

kamal
  • 1,093
  • 6
  • 19
  • 34
  • 1
    are you sure you well ever get to the part of code marked "dead"? – elyashiv Aug 14 '12 at 12:25
  • 2
    We need some more (real) code to see, why this warning popped up. Usually you have something like `System.exit(0); System.out.println( 'blab' );`, where the second command is never reachable at execution. – Sirko Aug 14 '12 at 12:26
  • 1
    suppose you have myCondition1 = true and myCondition2 = !myCondition1. – brano Aug 14 '12 at 12:26
  • 1
    could you post the code before the `if` statment? Are you forcing the value of one of myCondition to false? – M. Abbas Aug 14 '12 at 12:27
  • 1
    @Sirko In principle you are right but `System.exit()` doesn't affect reachability. See http://stackoverflow.com/questions/11487184/why-is-return-needed-even-after-system-exit0 – biziclop Aug 14 '12 at 12:27
  • post your sample codes where did you face the warning problem, warnings may appear due to listed reasons,mention code does not provide much information – thar45 Aug 14 '12 at 12:44
  • Probably because it was written in Java. – Sentinel Sep 12 '12 at 14:00

5 Answers5

6

"Dead code" is code that will never be executed. Most likely one of your conditions is hard-coded to false somewhere, making the conditional inside the if always false.

Dylan
  • 13,645
  • 3
  • 40
  • 67
3

Dead code means it is never going to execute. E.g.

void someMethod() {
    System.out.println("Some text");
    return;
    System.out.println("Another Some text"); // this is dead code, because this will never be printed
}

Same in case of your condition checking e.g.

String obj = "";
if(obj == null && obj.equals("")) { // here you get warning for Dead code because obj is not null and first condition is false so obj.equals("") will never evaluate

}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

Your code inside the block is never reached. The reason is most likely that one of the conditions is always false.

Petteri H
  • 11,779
  • 12
  • 64
  • 94
0

If one or more of myCondition1, myCondition2 and myCondition3 are always false (like private const bool myCondition1 = false;) then that code inside the if will never be executed.

victorvartan
  • 1,002
  • 2
  • 11
  • 31
0

This could occur for a number of reasons. Either the whole of the if block is dead, caused by something like the following:

boolean condition1 = true;
boolean condition 2 = !condition1;

if(condition1 && condition2) {
    //This code is all dead
    Foo f = fooFactory();
    f.barr(new Bazz());
}

Or you unconditionally leave the if block using something like return, break or continue, as shown below:

for(Foo f : foos) {
    if(true) {
        f.barr(new Bazz());
        break;
        //Everything after here is dead
        System.out.println("O noes. I won't get printed :(");
    }
}
Edd
  • 3,724
  • 3
  • 26
  • 33