Polymorphic Dispatch - It's Not Just a Good Idea, It's the Law

Location: Portland 256
Average rating: ***..
(3.61, 18 ratings)

I’m sure we’ve all written code that’s riddled with repeated conditionals and null checks. Code that treats our objects as data structures… But what happens when we take seriously the idea that objects are islands of functionality that we send messages to?

I find that my code gets cleaner. Here’s the basic driving code of a web app I maintain:

sub call { my($self, $env) = @_; my $req = $self→parse($env) or return $self→bad_request($env); my $resource = $self→fetch($req→id) or return $self→not_found($req); my $selections = $resource→filtered_by($req→filter); unless (@$selections) { return $self→not_found($req); } $selections→tokenize; $self→formatter_for($req)→render($selections); }

Here it after I remembered the importance of polymorphic dispatch:

sub call { my($self, $env) = @_; my $req = $self→parse($env); my $resource = $req→fetch_from($self→store); my $selections = $resource→filtered_by($req→filter); $selections→tokenize; $selections→print_on($self→formatter_for($req)); }

The conditional behaviour is still there, but responsibility for that behaviour is passed out to the objects that know what’s going on for this particular request.

In this talk I show how thinking about the messages you send to your objects and the way that they are dispatched to actual method definitions that do such will help you to eliminate conditional and structural code from your methods and express your intent more clearly in your code.

Photo of Piers Cawley

Piers Cawley


Piers Cawley started programming Perl in the mid nineties, but recently spent a few years working as a Ruby programmer before returning to Perl.

He’s currently trying to escape to become a bread maker.

He’s a singer, baker and photographer.

Comments on this page are now closed.


Picture of Ricardo Signes
Ricardo Signes
08/01/2011 12:06am PDT

How can I not like a talk that builds on what we can learn from Smalltalk? It was a good talk, and covered an important topic. I think the final(?) example, more or less using the null object for propagating failures caused a bit of unexpected controversy, which detracted from the potential impact of the idea, which was unfortunate to have so near the end. In general, though, this was a good talk that deserves repeating.

Picture of Piers Cawley
Piers Cawley
07/30/2011 12:50pm PDT

Jennifer: Believe me, I know.

I had intended to come to OSCON with slides all finished. I knew what I wanted to say, but I was horribly blocked on how I was going to say it. Slides were finished the night before the conference and I stand as an object lesson in why You Should Not Do This. I hope it wasn’t too hard to follow and, as soon as the fog of travel has cleared, I’ll be writing the talk up as a blog post so you can go through it at a more leisurely pace and where I hope I’ll be able to write a bit more than the sketch I presented.

The bug in the JavaScript slide is deeply embarrassing though – I went through that deck proofreading about 10 times.

Shawn Page
07/29/2011 1:06am PDT

This was a little hard to follow at times, accompanied by some hyperbolic exaggeration of pet peeves. Having given two stars for the style of the presentation, I am not averse to swimming in deep water and give four stars for the content, ideas and reading suggestion (Smalltalk Best Practice Patterns). I left with plenty to think, review and study. Well done. P.S. If I promise to only watch Top Gear, can I get the BBC iPlayer in the States?

Picture of Augustina Ragwitz
Augustina Ragwitz
07/28/2011 8:15am PDT

I really enjoyed the talk! Cawley introduced an interesting challenge, write code without exceptions or if statements. The examples were a little too abstract for me, however. I would have really preferred examples that were simpler and more concrete such that I could fully grasp the context. For instance, writing a class from scratch to do something simpler than processing web content to better illustrate polymorphic dispatch. I’m an intermediate level Perl developer and found some parts of this to be a little over my head, however I appreciated Cawley’s enthusiasm and I will definitely be reading more to better understand it.

Jennifer Cohen
07/28/2011 7:45am PDT

Take a lesson from the man who introduced you, and work on your slides!! Second lecture in a row that you said, oops, should have debugged my slides. It is sooo hard to follow a lecture where I have to adjust my brain while looking at your examples.