Java Reference
In-Depth Information
int p1 = foo();
int p2 = bar();
if (baz()) {
return p1;
} else {
p2 = p1;
}
return p2;
Compliant Solution
This example can be corrected in many different ways depending on the intent of the pro-
grammer. In this compliant solution, p2 is found to be extraneous. The calls to bar() and
baz() could also be removed if they do not produce any side effects.
Click here to view code image
int p1 = foo();
bar(); /* Removable if bar() lacks side effects */
baz(); /* Removable if baz() lacks side effects */
return p1;
Applicability
The presence of dead code may indicate logic errors that can lead to unintended program
behavior. The ways in which dead code can be introduced into a program and the effort
required to remove it can be complex. As a result, resolving dead code can be an in-depth
process requiring significant analysis.
Inexceptional situations, dead code may make software resilient tofuture changes. An
example is the presence of a default case in a switch statement even though all possible
switch labels are specified (see Guideline 64 , Strive for logical completeness , ” for an il-
lustration of this example).
It is also permissible to temporarily retain dead code that may be needed later. Such
cases should be clearly indicated with an appropriate comment.
The presence of code that has no effect can indicate logic errors that may result in un-
expected behavior and vulnerabilities. Unused values in code may indicate significant lo-
gic errors.
Search WWH ::




Custom Search