...
This compliant solution uses an Optional String instead of a String object that may be null. The Optional class ([API 2014] java.util.Optional) was introduced in Java 8 to make dealing with possibly null objects easier, see [Urma 2014] and can be used to mitigate against null pointer dereferences.
| Code Block | ||
|---|---|---|
| ||
public boolean isProperName(Optional<String> os) {
if (os.isPresent()) {
String names[] = os.get().split(" ");
if (names.length != 2) {
return false;
}
return (isCapitalized(names[0]) && isCapitalized(names[1]));
else {
return false;
}
}
|
The Optional class contains methods that can be used in the functional style of Java 8 to make programs shorter and more intuitive , as illustrated in the technical note by Urma cited above [Urma 2014].
Exceptions
EXP01-EX0: A method may dereference an object-typed parameter without guarantee that it is a valid object reference provided that the method documents that it (potentially) throws a NullPointerException, either via the throws clause of the method or in the method comments. However, this exception should be relied upon sparingly.
...