Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does Dead Code mean under Eclipse IDE Problems Section

Tags:

I am using Eclipse Helios IDE for our Web Application development. Under Problems section in Eclipse, for some of lines the description is displayed as "Dead Code".

Could anybody please tell me what does Dead Code actually mean ?

Please see the screen shot for your reference.

enter image description here

For example this part is shown as dead code under Eclipse

 else {         int length;         if (ar != null)             length = Array.getLength(ar);         else             length = 0; // This line is dead code 
like image 556
Pawan Avatar asked Jan 03 '12 14:01

Pawan


People also ask

Why is it showing dead code in Java?

A dead code is just a warning message native to Eclipse or another compiler. It is not displayed in the javac core java compiler. On the other hand, the unreachable code is an error reported by the javac Java compiler. It is officially included in java as an error.

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

What is logically dead code?

Logically dead code are branches of software that cannot be reached given the logical conditions. Finding logically dead code is important because it can indicate the software was not written as originally intended. So simply, a code change made the branch no longer necessary.


2 Answers

In Eclipse, "dead code" is code that will never be executed. Usually it's in a conditional branch that logically will never be entered.

A trivial example would be the following:

boolean x = true; if (x) {    // do something } else {    // this is dead code! } 

It's not an error, because it's still valid java, but it's a useful warning, especially if the logical conditions are complex, and where it may not be intuitively obvious that the code will never be executed.

In your specific example, Eclipse has calculated that ar will always be non-null, and so the else length = 0 branch will never be executed.

And yes, it's possible that Eclipse is wrong, but it's much more likely that it's not.

like image 145
skaffman Avatar answered Sep 21 '22 01:09

skaffman


Dead code is code that will never be executed, e.g.

 boolean b = true  if (!b) {     ....      // dead code here  } 
like image 45
THelper Avatar answered Sep 21 '22 01:09

THelper