I thought those terms where synonymous, but a note in MISRA regarding dead code indicates this to be wrong? What's the difference? Is one a subset of the other?
-
2Can you show the note?– user2357112Commented Apr 2, 2014 at 5:59
-
2"Note: unreachable code is not dead code as it cannot be executed"– Lord_GestalterCommented Apr 2, 2014 at 6:05
-
Assuming this is from MISRA-C:2012, read appendix J. Both dead code and unreachable code are explained.– LundinCommented Apr 2, 2014 at 6:35
5 Answers
Dead code - code that is executed but redundant, either the results were never used or adds nothing to the rest of the program. Wastes CPU performance.
function(){
// dead code since it's calculated but not saved or used anywhere
a + b;
}
Unreachable code - code that will never be reached regardless of logic flow. Difference is it's not executed.
function(){
return x;
// unreachable since returned
a = b + c;
}
-
So they are distinct and one is not the subset of the other? Commented Apr 2, 2014 at 6:30
-
@Lord_Gestalter Yes, they're distinct in definition. Both are redundant code, but the reason they're redundant is distinct. Commented Apr 2, 2014 at 9:06
-
So the empty statement ';' wouldn't be dead or unreachable code, since it's not executed but it could be reached (depending on the rest of the logic flow)?– jinaweeCommented Aug 16, 2018 at 9:55
-
1These definitions align with how I use the terms, but dead code is sometimes used as a synonym for unreachable code. Relevant Wikipedia articles: en.wikipedia.org/wiki/Unreachable_code , en.wikipedia.org/wiki/Dead_code Commented Mar 1, 2021 at 13:01
-
1This answer seems to be positing a more clear-cut answer than the usage supports. See for example the rust dead_code warning, which emits on unused functions. (doc.rust-lang.org/rust-by-example/attribute/unused.html)– SamCommented Jul 31, 2021 at 12:41
Dead Code
Code that performs functions that have no effect. Basically stuff that wouldn't make a difference if removed.
Unreachable Code
Code that due to other logic will never be executed. This is usually the sign of an error.
Unreachable code
The code to which control flow never enters during the execution of the program. That is unreachable code is that code that is never executed during the course of execution of the program.
Dead code
The code that has no effect on the codes following it no matter how the control flow flows through the program. That is dead code is that code, that doesn't need to be executed during the course of execution of the program, or in other terms, is useless.
So, in true terms none of them is a subset of another. But both unreachable code and dead code are usually removed by the compiler during compilation process as a part of code optimization.
-
1There is some confusing repetition in this text is in this text repetition confusing ;-)– WolfCommented Feb 25, 2015 at 16:40
-
The repetition is there to convey an additional idea. So, there is an additional idea in the repeated text. ;-) Commented Feb 26, 2015 at 7:20
-
Ah I see, but it's a little hard to get (as it is in real code) ;-)– WolfCommented Feb 26, 2015 at 8:45
unreachable code is something that would never be executed because there is no flow control to reach the code.
A dead code is something that gets (or might get) executed, but its results are never used.
Dead code refers to lines or code snippets that have no usage or references. For instance, imagine importing a third-party function, library, etc., but never using it anywhere in your solution or context.
On the other hand, unreachable code is code, for example in a method, that the program never runs, and it never affects the real-time project.
The difference is that dead code may have some side effects on the project, and it is parsed by the compiler, but unreachable code is never parsed or executed in the project.
-
Rarely have I seen an answer that is so wrong: an unused library is unreachable if it is never called; dead code cannot possibly have side effect, otherwise it isn't dead code... dead code is executed, but has no effects.– AndrewCommented Mar 17 at 8:22
-
@Andrew Rarely do I encounter a comment as caustic as this one! We weren't merely discussing a library or specific elements; we were addressing DEAD CODE – code that is unused, unnecessary, and overlooked amidst other lines. However, it's crucial to acknowledge that sometimes it can inadvertently impact the project in unexpected ways. Please be mindful of the context I emphasized: the PROJECT in question. If you're unsure about what I've mentioned, feel free to direct message me or consult Google, and let's strive to maintain a more courteous tone in our interactions. Commented Mar 18 at 10:41
-
But you post your (wrong) answer ten years late, when the correct answers are already here.– AndrewCommented Mar 18 at 14:54
-
2"DEAD CODE – code that is unused" NO NO NO... In the MISRA context (which is the topic) dead code is code that is used (ie executed) but has no effect.– AndrewCommented Mar 18 at 14:56