Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "dead code" and "unreachable code"?

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?

like image 684
Lord_Gestalter Avatar asked Apr 02 '14 05:04

Lord_Gestalter


People also ask

What does it mean when code is unreachable?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

What is considered dead code?

In some areas of computer programming, dead code is a section in the source code of a program which is executed but whose result is never used in any other computation. The execution of dead code wastes computation time and memory.

What does dead code mean in Eclipse?

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! }


2 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;
}
like image 197
domdomcodecode Avatar answered Oct 17 '22 00:10

domdomcodecode


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.

like image 4
Preet Sangha Avatar answered Oct 17 '22 01:10

Preet Sangha