Nick Ang profile picture

Nick Ang

What the duck is Duck Typing?

Boiled down to a sentence: it’s the choice of invoking a method based on whether something has the method, rather than based on whether something is of a certain type.

Use duck typing when you care more about what something can do instead of what something is.

For example, if you have a variable of unknown type, and you wish to take that value and concatenate it into a string, you might call the .join() method that comes with that variable type (e.g. Array). Now, instead of checking that it’s an array, you’d just check if the variable has the array method in its prototype chain (javascript lingo), and if it does, just call it.

I think duck typing relies a lot on trust. Trust that programmers name things well. If anything has been implemented with a method called .join(), then it better do something join-y, like combining an array into a single string. If it doesn’t, then kaboom, something will break!

what is duck typing?


Nick Ang profile picture
If something made you think, I would love to know.
contact  |  buy coffee