Causes
The existence of unreachable code can be due to various factors, such as:
- programming errors in complex conditional branches;
- a consequence of the internal transformations performed by an optimizing compiler;
- incomplete testing of a new or modified program that failed to test the bypassed unreachable code;
- obsolete code that a programmer forgot to delete;
- unused code that a programmer decided not to delete because it was intermingled with functional code;
- conditionally useful code that will never be reached, because current input data will never cause that code to be executed;
- complex obsolete code that was intentionally retained but made unreachable so that it could be revived later if needed;
- debugging constructs and vestigial development code which have yet to be removed from a program.
In the latter five cases, code which is currently unreachable is often there as part of a legacy, i.e. code that was once useful but is no longer used or required. However, the unreachable code may also be part of a complex component (library, module or routine), where the code continues to be useful in conjunction with different input data (never generated by the current application) or under conditions which are not met in the current runtime environment, thereby making the corresponding code unreachable, but which can occur in other runtime environments, for which the component has been developed.
An example of such a conditionally unreachable code may be the implementation of a printf function in a compiler's runtime library, which contains complex code to process all possible string arguments, of which only a small subset is actually used in the program. Without recompiling the runtime library, compilers will typically not be able to remove the unused code sections at compile time.
Read more about this topic: Unreachable Code