93.

Overview

When your program recursively descends a composite data structure so that occasionally more than one step down is taken via a single recursive call, you've come upon the Double Descent pattern. We discuss ClassCastExceptions and the conceptual errors behind this bug.

Although usually easier to debug than other, more insidious erroneous behaviors, ClassCastExceptions are often symptoms of conceptual errors in recursively descending a composite data structure. We'll discuss where programmers should look to find this pattern of bug, how to recognize the pattern, and what to do to minimize its occurrence.

Unlike the dreaded NullPointerException-which says nothing about what was expected to occur instead of the null pointer (see more in 10)-a ClassCastException is relatively easy to debug. A ClassCastException often occurs in a program that is performing a recursive descent over a data structure. I call this the Double Descent bug pattern.

Here's the breakdown of this bug pattern:

  • Pattern: Double Descent.

  • Symptoms: A program that throws a ClassCastException while performing a recursive descent over a data structure.

  • Cause: Some part of the code is descending two levels per method call without dispatching appropriately on the second descent.

  • Cures and Preventions: Factor the casting code out into separate methods for each class. Alternatively, examine the invariants to ensure that the casts will succeed.



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