Atlas · Details
When Polymorphism Fails
AI Notes
One of the earliest Drunken Blog Rants, from August 2004, when the column's audience was still mostly Amazon coworkers: it ribs a colleague by name, hands the reader a real Amazon interview question (the "eval" problem, brought in by Ron Braunstein) to try at home, and confesses to a third glass of wine right before the thesis lands. The hypothetical also has a hidden autobiography. Where the essay mentions online games that let players write their own code, the link points to cabochon.com — Steve's own company, whose game Wyvern had been running user-contributed monsters for years. The elf scenario came out of a system he actually operated, which is why he digs in so hard against the Fowler-approved answer he quotes at the top.
As a design essay it's cheerfully ragged. The code samples read like they were transcribed mid-pour; the Ruby thread-safety worry ends with Steve admitting he can't read the Japanese spec well enough to check; and the classic OO escape hatch, the Visitor pattern, never gets named even though his crack about creating "150 helper classes" walks right past it. He also treats the dilemma as unmapped territory when Philip Wadler had dubbed it the expression problem back in 1998, and Lisp's multimethods had been dispatching on the observer's terms for a decade before that. The complaint holds up anyway, and the detours reward the trip: a swipe at OCaml for shipping without a polymorphic print statement, objects that respond to delegation by goofing off watching Strong Bad clips, and a sign-off that calibrates blog length in glasses of wine — the exact finding is his to deliver.
Related listings
-
2008
The Universal Design Pattern
The constructive companion to this piece. Where Polymorphism Fails catalogues the cases the classical OO type system can't handle, the Universal Design Pattern offers the property-map approach Steve reaches for when inheritance gives out.
-
2006
Execution in the Kingdom of Nouns
Two years later — the funnier, more famous companion. Polymorphism Fails is the technical core of Steve's beef with Java-style OO; Kingdom of Nouns is the same beef as comedy.
-
2005
Choosing Languages
Same arc — the framework piece. Polymorphism Fails is one of the design lessons that shaped which languages Steve was steering toward in the column.
From the peanut gallery
Read the rest of the thread · 1 more
-
> polymorphism only makes sense when the polymorphic behavior
> is really a behavior of the target. When it's the behavior
> of the observer, you need runtime typing.
Is it runtime type-checking that is called for, or does it just look like that because Java overloads "instanceof" to test for type — and interface?
In Objective C, for example, you could use protocols (i.e. "interfaces") to implement the marker interface pattern. Objc Categories (a way of packaging extensions to a pre-existing class) would enable you to pretty cleanly extend the relevant monster classes from the comfort of the Elf interface file. You could then test it like so...
if ([creature conformsToProtocol: @protocol(AnnoysElves)]) { ... }Perhaps this approach would avoid inadvertently conforming to
@protocol(AnnoysMartinFowler).
Yeah, using Objective C protocols and marker interfaces is similar to Ruby's approach (Ruby stole the best stuff from -every- language...). In Ruby you can modify any class; they're always "open", and Ruby's been switching to something they call "duck typing", which is exactly the difference between conformsToProtocol() and instanceof. They call it respond_to?, but it's the same basic idea: they don't care what the overall type of an object is, just whether it responds to a message with the right signature. There's always the chance that it can be mistaken identity, but with reasonably specific method names you can avoid that pretty well.
I'm not sure if it would appease Fowler and friends, though, since it's either violating encapsulation (by mucking with classes at runtime), or polymorphism (by not mucking with them). I don't think they've addressed this situation in the books; maybe we should just ask him about it. :)
— Steve Yegge · September 22, 2004 05:06 AM