Versions Compared

Key

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

...

As shown in this noncompliant code example, a method modifies a node if it can find it in a linked list , and does nothing if the node is not in the list. But this method gives no indication if of whether it modified any node or not. A  method A method can silently corrupt the state of the object if it provides little communication describing what it did.

...

This solution returns a Node when one is found , and throws a NodeNotFoundException if the node is not available in the list.

Using exceptions to indicate failure can be a good design choice, but throwing exceptions are is not always appropriate. In general, a method should only throw an exception only when it is always expected to succeed...an succeed—an exception is only appropriate here only if the developer believes the method will always succeed.

...

 A return value that might be null is an in-band error indicator, which is discussed more thoroughly in ERR52-JG. Avoid in-band error indicators. This design is permitted , but is considered inferior to other designs, such as those shown in the other compliant solutions in this guideline.

...

Failure to provide appropriate feedback through a combination of return values, error codes, and exceptions can lead to inconsistent object state and unexpected program behavior.

Related Guidelines

...

Bibliography

...