What's the difference between “dead code” and “unreachable code”?

后端 未结 4 2313
野性不改
野性不改 2021-02-18 22:52

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?

4条回答
  •  没有蜡笔的小新
    2021-02-18 23:25

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

提交回复
热议问题