File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is dead code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is dead code?" Watch "What is dead code?" New topic
Author

What is dead code?

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all

I know for sure that following constitute a scenario of unreachable code:

a) once we throw an exception, any statement written after that is unreachable and the compiler complains. For example:



b) Writing a statement in a method after the return statement is unreachable.



What exactly is dead code then? Wikipedia says that In computer programming, dead code is code in the source code of a program which is executed but whose result is never used in any other computation. Could someone quote an example to make it clear?


~ Mansukh
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

in what situation would your line 5 of your second example run? As soon as you hit the "return" on line 4, you leave this method, so the answer is "none". There is no possible way you could ever get there.

The compiler assumes this is an error - why would you write code that cannot possibly execute?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Can you give an example of dead code Fred?
Andyy Smith
Greenhorn

Joined: Mar 07, 2013
Posts: 2
Dead code or "unreachable code" as some compilers call it is that which like you said is never executed. In my experience I have attempted to compile code that is "unreachable". Your example is already very clear



If a method is called then the method will run in a linear way until it reaches the end, or until it hits a return (like above). This 'return' will signify the end of the method and cause it to break and jump back to the code after the method call. Because of this, any code after it will not be run and cause the compiler to shout at you saying that there is "unreachable code". This is also referred to as '"dead code" by some programmers, although "dead code" can also refer to code that is written, but never used in any way. Some IDE's (such as Netbeans or Eclipse) will tell you that code is never used by underlining it in green.

An example of this:



Hope I didn't miss the point in your question!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hmm.. So, essentially what you are saying Andyy(why 2 "y"s? ) is that the warnings I get when I code in Eclipse IDE saying that "variable <i> is never read or used" is an indication that the code is dead. Correct ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37937
    
  22
Welcome to the Ranch Andy Smith

Yes, the yellow icon on Eclipse and the never used locally warning suggest that you may have unnecessary code, which some people call dead code.
Andyy Smith
Greenhorn

Joined: Mar 07, 2013
Posts: 2
Haha, yeah - had to settle for 'andyy' instead of 'andy'. Apparently there is already an 'Andy Smith' and it wouldn't let me register. I surf this forum quite a bit for answers and only signed up as I saw that your thread had no replies and I knew the answer!

But yeah, that's essentially correct! Do you have a block of code now that it's happening for? Or is this just a general observation that you've made?

Edit: Thanks Campbell
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

My confusion is clear now. Thank you Ritchie and Andyyyyyyy.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Mansukhdeep Thind wrote:My confusion is clear now. Thank you Ritchie and Andyyyyyyy.

Are you sure ? None of the answers you have been given equate to your quote from Wikipedia. Based on that article dead code and unreachable code are not the same thing. And even "variable <i> is never read or used" is not a warning of dead code - dead code is used.There's an example of dead code in the article.


Joanne
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

The IDE attempts to find dead code for you and when it's sure it will give you a failure, and when it's not so sure. However, there are other ways you an introduce dead code that the compiler won't catch. For example



Now if that is the only code you have then bar will never be called with b=false. So, ha() will never be called. It's dead
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I think dead code might refer to code which is executed but just a subset of its computed values is actually needed to solve your problem.
I think the main reason for that to happen is if you don't define your interfaces properly or but too much responsibility in one method/class.

The other reason I come to think of, you write an algorithm which is incredibly slow. You want to tell the user if it takes more than 2 seconds that he can go make a coffee.
Now someone else improves your code and it will never ever take that long. The code to tell the user to wait is still there but will never actually execute.
The same thing will happen if the average computational power available increases above a threshold.

Edit: The second one isn't dead code per your definition, as it never executes I still think its near enough to mention.


Please correct my English.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157


Are you sure ? None of the answers you have been given equate to your quote from Wikipedia. Based on that article dead code and unreachable code are not the same thing. And even "variable <i> is never read or used" is not a warning of dead code - dead code is used.There's an example of dead code in the article.


The article is saying that the calculation of iZ is an example of dead code because although it is calculated, it is not used anywhere. I know that unreachable code is different from dead code.


Ritchie said that the yellow warning that comes in Eclipse is an indication of dead code. What do you say Jo?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Just to add a bit to what is already said:

Eclipse appears to have a slightly different definition of 'dead code' when it gives its dead code warning. In Eclipse, the dead code warning indicates the marked source code never makes it to the compiled byte code. This usually happens because the compiler can optimize code and remove paths that can not be reached (or re-organize them). For example:

The two methods compile to the same byte code, because the compiler can analyze the source, determine that there is no way to reach the else statement, and so the remove it from byte code. Eclipse then marks the else statement in example1() as dead code (even though, despite the previous definitions) the code would never be executed.

[edit]
The compiler only does this when the un-reachable code is a product of optimization and not a programming error. For example, a statement after a return would be a product of a mistake, and there is no clear intention of what the intent was. An else statement which can't be executed can be optimized out since there is a clear intent on what should happen (follow the true case always), and the optimization does not have a side effect.


Steve
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Mansukhdeep Thind wrote:Ritchie said that the yellow warning that comes in Eclipse is an indication of dead code. What do you say Jo?

As I've already said, based on the Wikipedia article (or at least the start of it which is all I read) he's wrong.
Based on the Eclipse warning, he's right.

So, just like when people ask if Java is a pure OO language, it all depends on what your definition of dead code is.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Do you mean to say that different IDEs handle such scenarios differently? If I compile the same code from command prompt, will it still not make it to byte code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37937
    
  22
You should get the same bytecode from all different versions of the compiler.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Mansukhdeep Thind wrote:Do you mean to say that different IDEs handle such scenarios differently? If I compile the same code from command prompt, will it still not make it to byte code?

I'm not sure how you got that interpretation from what I said. What I meant was that you can't say if a piece of code is dead or not until you have defined what you mean by dead code. Wikipedia seems to have one definition, Eclipse another.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Mansukhdeep Thind wrote:Do you mean to say that different IDEs handle such scenarios differently? If I compile the same code from command prompt, will it still not make it to byte code?
Like Campbell said, you should get the same byte code. But what a compiler / IDE provides warnings for and what they display and how they name them could be different. I know Eclipse calls the above scenario dead code, I don't know if javac will report it, or any other IDE.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Joanne Neal wrote:
Mansukhdeep Thind wrote:Do you mean to say that different IDEs handle such scenarios differently? If I compile the same code from command prompt, will it still not make it to byte code?

I'm not sure how you got that interpretation from what I said. What I meant was that you can't say if a piece of code is dead or not until you have defined what you mean by dead code. Wikipedia seems to have one definition, Eclipse another.


It appears as if we were posting concurrently. I was referring to Steve's example. Jo. Thanks anyways..
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
Note that there is a precise definition of reachable and unreachable code in the Java Language Specification, section 14.21. This does not include all possible forms of dead code - as noted above, such definitions vary, and this is just one particular definition. But this is the definition that a Java compiler is required to use. Unreachable code, by this definition, is a compiler error, and the compiler is required to reject it. Other forms of dead code which are not unreachable, by this definition, are not compiler errors. Other tools like Eclipse, IntelliJ IDEA, PMD, etc. may well chose to give you warnings about them, and those warnings can be very useful to developers. But the compiler is not allowed to reject code based on these warnings - it only rejects based on JLS rules violations. Java's creators wanted to ensure that, as much as possible, the same code could compile and run in any environment.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
Campbell Ritchie wrote:You should get the same bytecode from all different versions of the compiler.

You typically get the same bytecode, but there is no guarantee of this. Different compilers can choose to compile the same code to different bytecode, as long as the effect of the code is the same. (Or, if they are fixing a bug in a previous version of the compiler.)
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

The Wikipedia definition sounds more like "deadbeat" code. It executes and uses resources but doesn't do anything useful.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
It looks like Wikipedia has separate articles for dead code and unreachable code. The latter is what the JLS talks about, though even here there can be differences in interpretation; code that is "reachable" according to the JLS may nonetheless be impossible to ever actually execute.
Criselda Clave
Greenhorn

Joined: Mar 07, 2013
Posts: 6
In computer programming, dead code is code in the source code of a program which is executed but whose result is never used in any other computation.[1][2] The execution of dead code wastes computation time as its results are never used.

While the result of a dead computation may never be used, the dead code may raise exceptions or affect some global state, thus removal of such code may change the output of the program and introduce unintended bugs. Compiler optimizations are typically conservative in their approach to dead code removal if there is any ambiguity as to whether removal of the dead code will affect the program output. The programmer may aid the compiler in this matter by making additional use of static and/or inline functions and enabling the use of link-time optimization.

Example

int foo (int iX, int iY)
{
int iZ = iX/iY;

return iX*iY;
}

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
See also dead horse.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37937
    
  22
Criselda Clave wrote: . . .

While the result of a dead computation may never be used, the dead code may raise exceptions or affect some global state, . . .
Where on earth did you find that? How can code which raises exceptions or alters global state be “dead”? Surely code which has side‑effects must be live code?

By the way: that shows the hazardous nature of side‑effects and global variables, but that is a different question.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37937
    
  22
I have found where you got that quote from.
I think it would have been better to call that example “apparently dead code”. It does have potential side‑effects, causing an exception if iY is zero.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
Yes - it came from the same Wikipedia article we've been talking about for some time, which I just linked to. Hence my "dead horse" comment - we're going in circles once again. And, as Joanne and I have pointed out, those articles draw a distinction between dead code and unreachable code. I don't know how pervasive or standard this distinction is out in the rest of the world, but at least they're consistent about it. Most people here seem to wish that the "dead code" definition sounded more like the "unreachable code" definition - but I do see value in having two terms for different things. Perhaps there's a better term for "dead code" that doesn't cause this confusion - but I'm happy enough just using the Wikipedia definitions. The JLS also uses "unreachable" in basically the same way Wikipedia does.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is dead code?
 
Similar Threads
Why Compiler Error.
Why is this not a compilation error?
please clear my doubt
Unreachable Code
unreachable code in 'finally'