DEV Community

Mike Barlow
Mike Barlow

Posted on • Originally published at bardev.com on

Duck Typing

In this post I will describe Duck Typing in regards to dynamic languages (JavaScript, PHP, Python, Ruby).

If you ever read or heard about Duck Typing, you have probably seen the following quote:

If it walks like a duck and quacks like a duck, it must be a duck.

Without context, this quote doesn’t mean much.  Hopefully at the end of this post the previous quote will make much more sense.

Below is an excerpt from Wikipedia.  Without understanding Duck Typing, the following excerpt doesn’t make much sense either.

Duck Typing requires that type checking is deferred to runtime, and is implemented by means of dynamic typing or reflection.

Duck typing is concerned with establishing the suitability of an object for some purpose. With normal typing, suitability is assumed to be determined by an object’s type only. In duck typing, an object’s suitability is determined by the presence of certain methods and properties (with appropriate meaning), rather than the actual type of the object.

https://en.wikipedia.org/wiki/Duck_typing

Duck Typing in JavaScript – Dynamic Language

In the following examples, we are going to create following 3 different objects in JavaScript

  • point2D
  • point3D 
  • me

All three objects  will include properties: name,  x and y, they  also include a calculate() function.  The Me object will include the properties occupation and birthDate. See image below for a visualization for object model.

image

In the code below, I created the three object (point2D, point3d, me).  I also created a function called calculate() for each object.  The calculate() function for all the objects returns a string. 

There is a displayPoint() function defined.  This function could just have been called display().  The function displayPoint() has one parameter called “point”.   The function signature looks like this:

function displayPoint(point)

The parameter name “point” doesn’t mean anything.  I could have called it “obj”.  I called the function “displayPoint” and the parameter “point” because contextually the function is created to work with points.  Even though I created the function displayPoint(point) to work with points, but I can pass any type of object as a parameter in to the function.

In function displayPoint(point) the code accesses the property “name” and the method “calculate()” of the object “point”. It doesn’t matter what type of object is passed in to the function “displayPoint”, but the object must have the property “name” and the method “calculate()” to run without an error.

This is Duck Typing.

What is the “me” object? Is it a person or a point – both, neither?  What do the properties x and y represents.  The properties x and y could represent a point, but they could also be pointers (reference) to XY chromosomes.  But in this context of the method “displayPoint”, the object “me”, and the properties x and y are points and treated like points.

Now, the following quote has context. I hope it makes more sense now.

If it walks like a duck and quacks like a duck, it must be a duck.

Resources

The post Duck Typing appeared first on Mike Barlow (BarDev).

Top comments (0)