Blogs

This Blog

News

Favorite Posts

Archives

ctodx

Discussions, news and rants from the CTO of DevExpress, Julian M Bucknall

DevExpress newsletter 45: Message from the CTO

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.)

Published Apr 06 2011, 07:00 AM by
Filed under:
Bookmark and Share

Comments

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: boo.codehaus.org/Duck+Typing

April 6, 2011 12:24 PM

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: www.srtsolutions.com/c-adds-duck-typing-latin-keyword

April 6, 2011 4:04 PM

About Julian Bucknall (DevExpress)

Julian is the Chief Technology Officer at Developer Express. You can reach him directly at julianb@devexpress.com. You can also follow him on Twitter with the ID JMBucknall.
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2014 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners