What are the questions that may flash across our minds when we look at a term "Dead Code" .. sounds strange, isn't it!!
Let us understand what significance does the term hold in the context of a software application.
Dead code is that part/section of code of a software program, that is executed but the computation result of the code is never use anywhere in the program. That is the code is in no way connected to the rest of the program. So why do we need a code that is unused ?
Need for Dead code :
Although the dead section of the source code is never used, such code may point out exceptions inside the code, so removal of such code may introduce unnecessary bug. So the unreachable portion of a source code may exist due to the following few factors :
- When there are too many conditional branches in a program
- When testing of a modified program remains incomplete
- Programmer forgets to remove some of the obsolete portions of the code
- Some of the code might have been intentionally retained in case it is required in future
Identifying Dead Code :
Detection of unreachable code is a kind of static analysis of a software program. It depends on the developer to perform a control flow analysis to ascertain whether there exists any unreachable or dead code within the program.
Types of Dead Code :
- Dead procedure or function :A dead procedure is the one that is no longer required or the function which serves no useful purpose, should be removed. If the dead procedure remain, it may produce bugs at a later stage in the program. For instance those procedures that has callers but the callers never execute, are the dead procedure.
- Dead Variable :A dead variable is simply the one that is written but never read. Those variables just cause memory overhead. A dead variable could be the one which is read only or write only. Those variables consume memory space without serving any useful purpose, the read instructions read the value but do not yield any result.
- Dead Parameter :The parameters passed to a function that are not really used by the function/method are termed as dead parameters. Suppose we pass boolean variables to a function that is expected to perform file overwrite. A call to the function to perform the task will simply check the conditions and perform the task accordingly. Hence the parameters, i.e, the boolean values act as dead parameters as they has no such role in the execution of the function.
- Dead Return Value :A return value which is not used by any other function. It may happen in situations like the calling function is not written correctly, that is, the callers are not fetching the return value. The second kind of scenario may be where the calling function is correct but the return value is useless. The function may be written as a void function.
- Dead Event Declaration :An event that does not trigger any action, is a dead event. The developers may face difficulties while executing a code as they may not get the expected results.
- Dead Class :The class that is not used anywhere is a dead class. The class may be executable but does not provide any output.
Ways to detect Dead Code :
- Manual Code Review - One can think of setting up a code analysis/review group to observe each class, interface, file etc. One can look at the code carefully and see at what point we are facing the issue of dead code.
- Automated Code Review Tool - Use of some tool to detect the presence of dead code. The automation tool records use of each programming object. After recording we get a list of all the parts which was not found. An efficient automation tool will offer robustness and complete code coverage, thus properly interpreting each line of code and producing the correct results.
How to eliminate Dead Code :
Removing dead code from a program is an optimization technique in which the presence of dead code is analysed with the help of live code analysis, which is a type of data flow analysis. This analysis checks for live variables before the next write activity.
Demerits of presence of unreachable code :
- Consumption of time and memory
- With repeated execution of code, the increase in the number of caching cycles leads to performance issues.
- Documentation and maintenance overheads.
Despite of the fact that dead code adds to the burden of a software program, a dead code is required in certain situations. A careful observation can help a developer too figure out the need of a dead code, if any. A very keen insight is what one needs to attain an edge.