Duck typing
“If it walks like a duck and it quacks like a duck, then it must be a duck”
What?
Duck typing is a type of dynamic typing, and a set of variables and methods of an object determines the type of an object. Instead of classifying types by class inheritance or interface implementation, duck typing considers an object to belong to that type if it has variables and methods that match that type. It is also a concept for implementing polymorphism.
Why?
Advantages of Duck Typing
Duck typing has plenty of positives. After all, there is a reason that so many newer languages allow this functionality. The first advantage is that it leads to fewer lines of code. This makes it look cleaner; thus making your code easier to read and faster to write.
It can be hard to see the advantages of duck typing until you have used languages with and without it. Afterward, you will realize it makes your job a lot easier when you’re not dealing with interfaces (such as in Java or C#) or keeping track of what type your object is. Assuming you don’t fall into bad practices, it really is useful functionality!
How?
This is an example which explain how the duck typing works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Idol
def initialize(skill)
@skill = skill
end
def performance
@skill.dance
end
end
class BlackPink
def dance
pp ‘Sexy dance’
end
end
class Bts
def dance
pp ’Smooth dance’
end
end
If we try those classes:
1
2
Idol.new(BlackPink.new).performance #=> "Sexy dance"
Idol.new(Bts.new).performance #=> "Smooth dance"