Saturday, May 24, 2008

Dynamic Languages such as Python, Ruby and Groovy, make a big dealof their flexibility. You can add new methods to classes, extendthem, etc... at run time, and do all kinds of funky stuff. You caneven treat an object as of a certain type by looking at it's methods.This is called DuckTyping: "If it quacks like a duck and swims like a Duck thenit's a duck", goes the well known saying. The main criticism ofDuck Typing has been that what is gained in flexibility is lost inprecision: it may be good for small projects, but it does not scale.I want to show here both that the criticism is correct, and how toovercome it.

Let us look at Duck Typing a little more closely. If something isa bird that quacks like a duck and swims like a duck, then why notindeed treat it like a duck? Well one reason that occurs immediately,is that in nature there are always weird exceptions. It may bedifficult to see the survival advantage of looking like a duck, asopposed to say looking like a lion, but one should never be surprisedat the surprising nature of nature. (Hestretches the analogy past its usefulness. It's not nature, its aconvenient name to wrap a computing idiom)Anyway, that'snot the type of problem people working with duck typing ever have.How come? Well it's simple: they usually limit the interactions oftheir objects to a certain context, where the objects being dealtwith are such that if any one of them quacks like a duck, then it isa duck. And so here we in essence have the reason for the criticism:In order for duck typing to work, one has to limit the context, onehas to limit the objects manipulated by the program, in such a waythat the duck typing falls out right.(Yesyou do, and this limitation is very rarely seen as a problem).Enlarge the context,(presumably towhere the objects being dealt with quack like a duck but are notducks), and at some point you will find objects that don't fitthe presuppositions of your code. So: for simple semantic reasons,those programs won't scale. The more the code is mixed and meshedwith other code, the more likely it is that an exception will turnup. The context in which the duck typing works is a hiddenassumption, usually held in the head of the small group of developersworking on the code. In short he issaying:

Use in a large programs will cause objects with the right signature but the wrong actions to be successfully called in Duck typing but give the wrong result.

Duck typing only works because the developers know what are compatible actions.

Only a small group of developers can know what are compatible actions.

A slightly different way of coming to the same conclusion, is torealize that these programming languages don't really do an analysisof the sound of quacking ducks. Nor do they look at objects and tryto classify the way these are swimming. What they do is look at thename of the methods attached on an object, and then do a simplestring comparison. If an object has the swim method,they will assume that swim stands for the same type ofthing that ducks do. Now of course it is well established thatnatural language is ambiguous and hence very context dependent. Themethods names gain their meaning from their association to englishwords, which are ambiguous. There may for example be a method namedswim, where those letters stand for the acronym "SeeWhat I Mean". That method may return a link to some page onthe web that describes the subject of the method in more detail, andhave no relation to water activities. Calling that method inexpectation of a sound will lead to some unexpected resultsButonce more, this is not a problem duck typing programs usually have.Programmers developing in those languages will be careful to limitthe execution of the program to only deal with objects where swimstand for the things ducks do. But it does not take much for thatpresupposition to fail. Extend the context somewhat by loading someforeign code, and at some point these presuppositions will break downand nasty difficult to locate bugs will surface. Once again, thecriticism of duck typing not being scalable is perfectly valid. Sohis criticism here is that:

Load 'foreign' code with the right method signature and it is likely to fail.

Lets take his points one by oneand show why Duck Typing can and does work for many people:

His point 1:

Thedifference between a large and a small program would be that the codeis so large that, objects are passed to functions based solely ontheir method signature compatability rather than on what thosemethods do, and what a function expects to do to the object.

As aprogrammer you need to know the functionality of what you puttogether. Small or large systems –you still need to know, youshould not pass it off, and only testing will show how right you are.

His point 2:

Turnit on its head “If the developers don't know what arecompatable actions, Duck typing doesn't work”.

Yes,for Duck typing to work, developers need to know about what they arelinking, but other processes, essential to the production of qualitysoftware will make this tracktable.

His point 3:

Followingon from the answer to his point 3, If you are not building a largesystem out of a collection of smaller ones with local, identifiableinterdependancies then it could be a problem but that isn't Ducktyping at fault – You have a mess that no one can understandbut are trying to force development on. Its not the size of thecodebase that is defeating Duck typing it is its haphazardorganization.

His point 4:

Itis wise to be wary of code you don't know the history behind. Inlarger projects you are more likely to use foreign code but exploringit for suitability for Duck typing is part and parcel of goodpractice. You should know about what you are using, Duck typing ornot. If you have programmers using code that is still 'foreign' tothem, then you cannot expect quality.

You could go on and read hissolution to a poorly stated problem in his article.

Paddy.

On careful reading of hisfollow-uparticle, in the section “Scalability and efficiency”,he turns things on their head by admitting that the original articlewas about Duck Typing not being a replacement for WebURI's. And they are not. We can agree on that. :-)