Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing & code tweaks

...

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

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

...

Code Block
bgColor#FFCCCC
String s;
String t;

// ...

s.equals(t);

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

Compliant Solution

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

...

Code Block
bgColor#FFCCCC
int p1,p2;
p1 = foo();
int p2 = bar();

if (baz()) {
  return p1;
}
else {
  p2 = p1;
}
return p2;

...

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() can could also be removed if they do not produce any side effects.

...