Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Duck Typing?

Tags:

c#

duck-typing

Question

I've now read a lot about Duck Typing, and I seem to understand the concept.

What I've not understood is, in what case it does effectively make sense to abandon the benefits of strong typified programming to the benefits of Duck Typing. In what case would one use Duck Typing instead of Interfaces and Inheritance?

I mean if you anyway need to secure that an object passed to a Method implements certain methods, why shouldn't I simply define an Interface?

Just to be clear, I know how Duck Typing works. I want to know when it really makes sense to use it.

Clarification:

In which case would you use

public bool MyMethod(dynamic obj)

instead of

public bool MyMethod(ISomeInterface obj)
//or
public bool MyMethod(SomeBaseClass obj)
like image 481
LuckyLikey Avatar asked Jun 03 '15 11:06

LuckyLikey


People also ask

Where do you use duck typing?

Duck Typing is a type system used in dynamic languages. For example, Python, Perl, Ruby, PHP, Javascript, etc. where the type or the class of an object is less important than the method it defines. Using Duck Typing, we do not check types at all.

What is the benefit of duck typing?

With duck typing the type doesn't matter as long as it has the right method, so it really just eliminates a lot of the hassle of casting and conversions between types. There is plenty of anecdotal evidence suggesting that duck typing is significantly more productive than static typing.

Why do we need duck typing in Python?

The main reason for using duck typing is to provide support for dynamic typing in Python programming. In Python, we don't need to specify the variable's data type and we can reassign the different data type values to same variable in further code. Let's see the following example.

What is the difference between duck typing and inheritance?

With nominative typing, an object is of a given type if it is declared to be (or if a type's association with the object is inferred through mechanisms such as object inheritance). In duck typing, an object is of a given type if it has all methods and properties required by that type.


2 Answers

C# has strong typing for a reason. Unless you have a valid reason (such as needing COM interop) to use the dynamic type, you should probably avoid it like the plague, or you'll risk turning compile-time problems into runtime problems. dynamic is powerful, but easy to abuse. Think long and hard about if you really need dynamic typing - if you think there is, there's a chance that you're approaching the problem wrong to begin with, and need to refactor your code.

To specifically answer your question - one potential use case was if you were writing serialisation code and needed to accept a deserialised object, for example a deserialised JSON structure from a web API request. Such methods would need to handle any type given to them, which is a situation in which using dynamic is better than the alternative (ie. a truck-load of reflection).

Another example I can think of would be interoperability with languages specifically on the Dynamic Language Runtime (eg. JavaScript, IronPython, IronRuby, ...), and needed to write a method accepting types from such languages.

From Beginning Visual C# 2012 Programming:

For most C# code that you write, avoid the dynamic keyword. However, if a situation arises where you need to use it, use it and love it - and spare a thought for those poor programmers of the past who didn't have this powerful tool at their disposal.

like image 96
Tom Galvin Avatar answered Oct 16 '22 17:10

Tom Galvin


Duck-typing is used a lot in C#, you're just unaware of it most of the time. The compiler uses it a lot under the cover, for foreach statements, for Linq, for awaitand for collection initializers. This question details it nicely.

The other way for you to use duck typing is with the dynamic keyword. And let's be franc, you should avoid it as much as possible. But it's very useful to interoperate with dynamic languages/contexts. For instance, suppose you're calling a web service that answers with poorly defined json (so you can't deserialize it easily to a known class). It might be a lot easier for you toparse it as a JObject using json.Net, and use that JObject as a dynamic.

dynamic myParsedJson = JObject.Parse(json);
string theStringImLookingFor = myParsedJson.foo.bar.blah.nicestring;

Another use case is in ASP.net MVC Viewmodels. Having a dynamic viewmodel can be very powerful. The Orchard CMS makes heavy use of it, and even thoug it's a little hard to wrap your head around it at first, it allows very powerful use cases.

COM interop comes to mind as well.

like image 26
Falanwe Avatar answered Oct 16 '22 19:10

Falanwe