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

断了今生、忘了曾经 提交于 2019-11-30 17:32:20

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!