Duck Typing
The programming term, duck-typing originated from the famous phrase:
If it looks like a duck, walks like a duck, and quacks like a duck, it’s a duck.
Duck typing is a style of dynamic typing which focuses on the behavior of an object rather than what it actually is. The behavior of an object is determined by the method it responds to and the properties/attributes it posses, which are just reader methods.
It is most commonly seen in dynamically typed languages where the type checking happens at run time rather than at compile time like static languages. However, it is possible to duck type in static languages using some sort of reflection.
In dynamically typed languages we don’t specify the data type for parameters or variables like we do in languages like C or Java. Instead we declare the variable and then assign it a value, no matter the type. Then call methods on that variable/argument and hope that it behaves as expected.
For example, say we had two pets, cat and dog, and we wanted to create a program to automated the task of feeding them. Instead of writing something like this:
def feed_pet(pet)
if(pet.class === Dog)
give_bone
else if(pet.class === Cat)
give_tuna
end
end
Where we are constantly checking the type of the argument to determine what to feed them.
We can use use duck typing and turn that to this:
class Dog
def eat
eat_chicken
end
endclass Cat
def eat
eat_tuna
end
enddef feed_pet(pet)
pet.eat
end
Instead of checking what the type of the pet is, just check if the pet can eat. If we follow this same blueprint for our pets we can add more without having to conditionally check each one.
duck typing vs. polymorphism
You might be thinking that these are the same thing and though they do have some similarities they are not.
Polymorphism, in the context of OOP, means that a subclass can override a method from the base class. Meaning that the same method from the parent class is different from the subclass’s method.
As opposed to duck typing, polymorphism actually cares about the type of object at hand.
For example, Dog and Cat instances will inherit the method talk()
from the Animal class. However, that same method when called on dogs and cats will behave differently if redeclared in the class definition.
class Animal
def talk
puts 'animal sounds'
end
endclass Dog < Animal
def talk
puts 'woof'
end
endclass Cat < Animal
endDog.talk => 'woof'
Cat.talk => 'animal sounds'
Duck typing just cares if the talk()
method works on the object rather than if it was inherited from another class or not.
There are some languages which support polymorphism but not duck typing, like Java. And other languages which support both like Ruby.
Conclusion
Duck typing is simply a way of thinking in dynamically typed languages. Where the behavior of an object is more important that the type. It can be confused with polymorphism, another common feature of object-oriented languages, but there is a clear distinction between the two.
Though duck typing can have some benefits like: giving programmers more flexibility and cutting down on development time. It is commonly looked down upon as it can introduce major bugs in the future as the application scales up in features. Use at your own risk.