May 2003

The Dynamic Dispatch Problem

Introduction

Example Problem

Does this ever Happen?

Reasons for the Problem

    1. The fact that class A is using dynamic dipatch from setRange() to setRangeLength(): The programmer of A should have made this a direct method invocation instead. Yet this solution creates its own problems: A's test suite may work perfectly fine on class A as written above, how can we expect A's programmer to know there is an error in the specification of A?
    2. B.setRangeLength() is calling A.setRange(): The mear fact that B is calling a method of A is one cause of the problem. We could have the B's programmer indicate this call as immune to dynamic dispatch.
    3. A.setRangeLength() is not "final": Preventing A.setRangeLength() from being overridden prevents B.setRangeLength() from ever existing. In general, all methods that are called should be declared "final", but this is an extreme that inhibits inheritance.
    4. Dynamic dispatch is allowed across project boundaries: By indicating that A belongs to a different project we can disallow dynamic dispatch across the project boundary. This option may be too harsh because there could be situations that this dymanic dispatch is good.
    5. Complier gives no warnings: We can blame the compiler for not identifying the logical loop created by the two classes. Identification can flag the suspects and have B's programmer change his use of A's methods, specifically call setRangeLength() instead of setRange(). But how can the compiler tell the difference between intended recursion and these unintended loops?

Solutions


May 7, 2003: Revisions and formatting

Februray 22, 2003: First writing

April 10, 2003: Clarified and formatted