| What We've Learned In this chapter on the Broken Dispatch bug pattern we've learned the following: -
Broken Dispatch can be described as follows: (1) The arguments to an overloaded method (method1) are passed to another method (method2) that takes more general types; (2) method2 invokes method1 with these arguments; (3) Because the static types of these arguments inside the scope of method2 are more general, the wrong version of method1 might be invoked. -
Broken Dispatch errors can be difficult to diagnose because they can be introduced simply by adding new methods. Also, program execution may continue for some time before problems are discovered. -
In a test-laden environment, the most common symptom will be a test case for code you haven't touched that suddenly breaks. If the test case breaks immediately after you overload another method, this bug pattern is almost certainly the culprit. -
The most straightforward cure for this bug is to upcast the arguments in the method invocation. A good preventative measure is to avoid overloading methods. Remember, argument structure is a key component to assuring that when you overload or override a method, the call invokes the intended method. In Chapter 15, we'll discuss the Impostor Type and the problems that occur when tags in fields—intended to distinguish object types—mislabel the associated data. |