Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixed color of compliant solution

...

This compliant solution demonstrates that the context in which code appears can impact its compliance. This example includes the same isName() method as the previous noncompliant example, but as part of a more general method that tests string arguments. 

Code Block
bgColor#FFcccc#ccccff
public class Foo {
  private boolean isName(String s) {
    String names[] = s.split(" ");
    if (names.length != 2) {
      return false;
    }
    return (isCapitalized(names[0]) && isCapitalized(names[1]));
  }

  public boolean testString(String s) {
    if (s == null) return false;
    else return isName(s);
  }
}


The isName() method is a private method with only one caller in its containing class. The calling method, testString(), guarantees that isName() is always called with a valid string reference. As a result, the class conforms with this rule, even though isName() in isolation does not. In general, guarantees of this sort can be used to eliminate null pointer dereferences.

...

[API 2006]

Method doPrivileged()

[Hovemeyer 2007]

 

[Reasoning 2003]

Defect ID 00-0001

 

Null Pointer Dereference

[SDN 2008]

Bug ID 6514454

 

...