Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The presence of unused values in code may indicate significant logic errors. To prevent such errors, unused values should be identified and removed from code.

Noncompliant Code Example (Dead Code)

This noncompliant code example demonstrates how dead code can be introduced into a program [Fortify 2006]:

...

The condition in the second if statement, (x != 0), will never evaluate to true because the only path where x can be assigned a nonzero value ends with a return statement.

Compliant Solution

Remediation of dead code requires the programmer to determine not only why the code is never executed but also whether the code should have been executed, and then to resolve that situation appropriately. This compliant solution assumes that the dead code should have executed and consequently removes the return from the body of the first conditional statement.

Code Block
bgColor#ccccff
int func(int condition) {
    int x = 0;
    if (condition) {
        x = foo();
        /* Process x */
    }
    /* ... */
    if (x != 0) {
        /* This code is now executed */
    }
    return 0;
}

Noncompliant Code Example (Dead Code)

In this example, the length() function is used to limit the number of times the function string_loop() iterates. The condition of the if statement inside the loop evaluates to true when the current index is the length of str. However, because i is always strictly less than str.length(), that will never happen.

Code Block
bgColor#FFCCCC
public int string_loop(String str) {
  for (int i=0; i < str.length(); i++) {
    /* ... */
    if (i==str.length()) {
      /* This code is never executed */
    }
  }
  return 0;
}

Compliant Solution

Remediating the dead code properly depends on the intent of the programmer. Assuming the intent is to do something special with the last character in str, the conditional statement is adjusted to check whether i refers to the index of the last character in str.

Code Block
bgColor#ccccff
public int string_loop(String str) {
  for (int i=0; i < str.length(); i++) {
    /* ... */
    if (i==str.length()-1) {
      /* This code is now executed */
    }
  }
  return 0;
}

Noncompliant Code Example (Code with No Effect)

In this noncompliant code example, the comparison of s to t has no effect:

...

This error is probably the result of the programmer intending to do something with the comparison but failing to complete the code.

Compliant Solution

In this compliant solution, the result of the comparison is printed out:

Code Block
bgColor#ccccff
String s;
String t;

// ...

if (s.equals(t)) {
  System.out.println("Strings equal");
} else {
  System.out.println("Strings unequal");
}

Noncompliant Code Example (Unused Values)

In this example, p2 is assigned the value returned by bar(), but that value is never used:

Code Block
bgColor#FFCCCC
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 programmer. 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.

Code Block
bgColor#ccccff
int p1 = foo();

bar(); /* Removable if bar() does not produce any side effects */
baz(); /* Removable if baz() does not produce any 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.

In exceptional situations, dead code may make software resilient to future changes. An example is the presence of a default case in a switch statement even though all possible switch labels are specified (see 4.1564. Strive for logical completeness for an illustration of this example).

...

Code and values that have no effect can be detected by suitable static analysis.

Bibliography

[Fortify 2006]Code Quality, "Dead Code"
[Coverity 2007]Coverity Prevent User's Manual (3.3.0)

...