DevExpress newsletter 45: Message from the CTO

06 April 2011

Boy oh boy, Jeff and I had fun videoing and editing this one: Duck typing.

Duck Typing

If you've done any programming with a loosely-typed scripting language for a while -- languages like JavaScript for example -- where the cost of declaring and creating some object is virtually free both in terms of programming resources (the amount of code you write) and run-time requirements, then you will certainly have run into duck typing.

Imagine that you have a function which expects a duck object. However in this method all that's really required is the Walk method of the passed in object, the Swim method and the Quack method. The function doesn't care if the object has a Bill property or a Feathers property or anything like that; it just wants to ask the object to Swim, Walk, and Quack. If the object can't do one of these things then a run-time error is raised.

In essence what's happening here is exemplified by the following saying: "if it walks like a duck, swims like a duck, and quacks like a duck, then I call that thing a duck."

Now you may say that duck typing is a little too dangerous to use. An example is a function expects an object with a Cleave method. In certain scenarios, Cleave may split something apart, and in others stick something together. The behavior changes according to the object passed in. In reality such issues are rare and can be avoided by proper testing.

The other thing to grasp is that duck typing is all about viewing object-orientation in terms of message passing, not about designing and implementing some class and interface model. It is ideally suited to a dynamic programming language with loose typing and provides a basic form of polymorphism without the verbosity and strict typing of an inheritance class model.

If you program in JavaScript or Ruby or Python, I would recommend that you eschew standard class-model-based object orientation and embrace the freedom of duck typing.

Not much more to say really. When objects are easy to create and configure (that is, without all the paraphernalia of a class model behind it), duck typing will be prevalent.

(You can watch the video here.)

Free DevExpress Products – Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We’ll be happy to follow-up.
Felipe R Machado
Felipe R Machado

Very interesting message! People from strong-typed languages sometimes forget that there are many more ways to implement polymorphism and other OO features. I strongly urge people here to take a look at how duck typing is implemented in BOO, which is a strong-type .NET language. Apart from being a very nice language, its creators and contributors have a really good sense of humor, for example, to implement duck typing you must implement an interface called IQuackFu... If it quacks like a duck...

Read it here:

6 April 2011
Toni Wenzel (exsportance)
Toni Wenzel (exsportance)

If anyone wants to know, the C# language team is working on a duck typing implementation. Right, duck typing in a strong-typed language.  They call it "robust duck typing".

Read here:

6 April 2011

Please login or register to post comments.