• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is dead code?

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give an example of dead code Fred?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My confusion is clear now. Thank you Ritchie and Andyyyyyyy.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should get the same bytecode from all different versions of the compiler.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Master Rancher
Posts: 4830
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Wikipedia definition sounds more like "deadbeat" code. It executes and uses resources but doesn't do anything useful.
 
Mike Simmons
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4830
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also dead horse.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic