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

Lord_Gestalter picture Lord_Gestalter · Apr 2, 2014 · Viewed 10.9k times · Source

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?

Answer

domdomcodecode picture domdomcodecode · Apr 2, 2014

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