standardsstandards-compliancemisra

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?


Solution

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