19.

Overview

When code that uses recursively defined data types signals an exception and the base-case definitions are handled inconsistently by client code, there may be a Dangling Composite bug; we discuss the core of the problem, the NullPointerException.

Dangling composites result from defining a recursive data type in such a way that certain base cases of the definition are not given their own classes. Instead, null pointers are inserted into the various composite data types. Instances of the data type are then used as if these null pointers were filled in properly. I call this the Dangling Composite bug pattern because the offending code is an application of the Composite design pattern that is imperfect because the composite data types contain dangling references (that is, null pointers).

Here's this bug pattern in a nutshell:

  • Pattern: Dangling Composite.

  • Symptoms: Code that uses a recursively defined data type is signaling a NullPointerException.

  • Cause: The recursive data type is defined in such a way that certain base cases of the definition are not given their own classes. Instead, null pointers are inserted into the various composite data types. The base cases are then handled inconsistently by client code.

  • Cures and Preventions: Ensure that the base cases are represented and checked for consistency. Give each base case its own class.

start sidebar
The Composite and Singleton Design Patterns

As mentioned in this chapter, the Dangling Composite bug pattern is an incomplete application of the Composite design pattern.

The Composite pattern lets you build complex objects by recursively composing similar objects in a tree-like manner. This pattern also allows the objects in the tree to be manipulated in a consistent manner by requiring all of the objects in the tree to have a common superclass or interface.

The Singleton pattern is also referenced in this chapter as a potential optimization pattern. This pattern ensures that only one instance of a class is created. All objects that use an instance of that class will use the same instance.

For more information on design patterns, see the Resources chapter.

end sidebar



Bug Patterns in Java
Bug Patterns In Java
ISBN: 1590590619
EAN: 2147483647
Year: N/A
Pages: 95
Authors: Eric Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net