web 2.0

Duck Typing

catduck If you ever played around with dynamic languages then chances are that you heard of something called Duck Typing. Yes, it is a very strange term but if you know the story behind it then the concept is very easy to digest. If you look up Duck Typing on Wikipedia you will get this definition:

Duck Typing is a style of dynamic typing in which an object's current set of methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

The most popular phrase used to explain duck typing is..."If it walks like a duck and quacks like a duck, it must be a duck"

Still confused? Then lets clarify the concepts with a little bit of ruby code:

class Duck
def quack
puts "Quack Quack!"
end
end

class ConfusedTurkey
def quack
puts "Gobble Quack Gobble"
end
end

 

So after some observation you will notice that we have 2 distinct classes. There is no inheritance between them and they each have a quack method. So, what if I instantiated one instance of each class and put it in an array, looped over the array and called quack? ...

quackers = [Duck.new(),ConfusedTurkey.new()]
quackers.each { |i| puts i.quack() }

 

Depending on your programming background you may be surprised at the output. Why? because in a static language you would have to cast the object to the correct type before invoking it. Otherwise, you would probably get a compiler or runtime error. However, with a dynamic language your objects do not have to support a method in order for it to work. When you run the ruby code above you will get the following output:

Quack Quack!
Gobble Quack Gobble

Just to spice things up a bit, lets create a third class called Fish. To my knowledge, a Fish can not quack (lol):

class Fish  
end
 

Now, lets modify the array so we include a Fish object. Then we will call quack on all three objects:

quackers = [Duck.new(),ConfusedTurkey.new(), Fish.new()]
quackers.each { |i| puts i.quack() }


Now what happens?

Quack Quack!
Gobble Quack Gobble
undefined method `quack' for #<ConfusedTurkey:0x8f3d790>

Ok. that makes sense. The fish can not quack so we get an error. However, the point it that Ruby still attempted to make the Fish quack. It checked to see if the Fish has a quack method and reported an error when it was not found. Surprisingly enough, we can get around this problem in Ruby using a special trick, the method_missing method. Method_missing is a special method in Ruby that is invoked when a method is called on an object that does not exist. So we will modify the Fish class to look like this:

class Fish
def method_missing( sym, *args, &block )
puts "I don't know how to " << sym.to_s
end
end
 

Now when we run that same bit of code we get the following output!

Quack Quack!
Gobble Quack Gobble
I don't know how to quack

Pretty cool eh?

So, why did I decide to write an article on duck typing. Because I think that its important for people to realize that although static languages can prevent people from doing unusual things they also have the ability to slow down productivity. If I want to create an array of 50 different objects and call quack on it then let me! A warning is acceptable, but don't prevent me from running it. Perhaps I have a good reason for doing it.

At the end of the day, your job as a programmer is to determine what is the right tool for the job. Learning multiple languages is like adding new tools to your toolbox. The worst case scenario is that you will learn something new or see things in a different light. After all, learning languages that seem exotic to you will generally make you a better programmer in your language of choice.

Comments

Jason Snelders Australia, on 1/10/2010 6:42:47 PM Said:

Jason Snelders

That's a very nice, simple and elegant explanation and example of Duck Typing - thanks!

I like the idea of dynamic languages and duck typing certainly has appeal. However, experience has taught me most programmers have trouble creating a well designed application with static languages (I'm of the opinion the industry would be better off without 98% of the programmers) so I can't help but think dynamic languages are just another way of making like harder for the rest of us that have to clean up their crap.

Ultimately I wonder if dynamic languages allow programmers to be lazy more easily?

Michael Ceranski United States, on 1/10/2010 8:19:49 PM Said:

Michael Ceranski

Hi Jason,

Thanks for leaving a comment. It is great to get some feedback...

I think dynamic languages give you flexibility. Or like the old saying goes they "give you enough rope to hang yourself". In other words, the programmer needs to be disciplined when using things like duck typing. However, if used properly it can be an extremely powerful tool.

As an experienced programmer you will find yourself put into a lot of situations where you will have to clean up other peoples code. However, as a consultant I find that this is my number one source of income. People start projects, realize they are in over their head, and then they call me to clean it up...LOL

I don't think dynamic languages make someone lazy, I think they just allow people to be more creative in their approach to a problem. It is nice to have options and sometimes I feel like static languages have the tendency to hold me back.

Comments are closed