Aaron Anodide
Aaron Anodide

Reputation: 17196

Have practices regarding on the use of extension methods changed or are there two schools of thought?

When I first learned about extension methods I read this:

In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type.

However, I have numerious times seen a very liberal use of extension methods in various production code bases.

Granted, my experience is not representative of the majority but I would like to know if there's a shift in the guidelines, an alternate design philosophy, or if I just happened to see enough code that ignored the guidlines to make me think so?

NOTE: I am not trying to spark a debate (which will promptly lead to this question closing) - I've honestly been wondering about this for a while and feel my best chance at getting an answer is here on SO.

Upvotes: 2

Views: 383

Answers (4)

Jin-Wook Chung
Jin-Wook Chung

Reputation: 4344

I think that there is pros and cons about using the extension methods.

Pros: The extension methods could be regarded as the visitor pattern(GoF). So, it takes advantage of the pattern, which is, without modifying code, we can extend some features.

Cons: Despite the advantage , why the MSDN tells using the extension methods sparingly is that, IMO, the extension methods would cause problem at some point. First, if the object for extension methods has different namespace from the extension, we should know where it is. It causes that sometimes we cannot use some important features from the extension. Moreover, I saw lot of code with extension abuse. Since extension is based on Type, sometime an object of that Type really do not need the extension, but when coding, we should see lots of extension method.


[update]

IMO, abuse about the extension methods

  1. using so general type for extension method, like object: if there is lots of extension methods with object type and that extension is only focused on few types, it'll annoy us, as every object is linked with the methods.
  2. broken linking with dot operator or misconception: Let me show an example. There is the code like below and we should call the Read and then Write method in order. However, we can call the methods in opposite order.

    public static string Read(this string message)
    {
        //do something
        return message;
    }
    
    public static string Write(this string message)
    {
        //do something
        return message;
    }
    
    public static void Method()
    {
        "message".Read().Write();
        "message".Write().Read(); // this is problem!
    }
    

Upvotes: 1

user981225
user981225

Reputation: 9262

Polymorphism does not work with extension methods as they are bound in compile time.

ParentClass parentClass = new ParentClass();
ParentClass derivedClass = new DerivedClass();

If both of these classes have an extention method called ExtensionMethod() (I have defintely seen extension methods attempting to mimick virtual/overriden methods), both of these calls would call the parent class's extension method:

 parentClass.ExtensionMethod();
 derivedClass.ExtensionMethod();

In order to actually use the derived extension method, you would have to call:

 ((DerivedClass)derivedClass).ExtensionMethod();

Upvotes: 2

Alexei Levenkov
Alexei Levenkov

Reputation: 100610

Extending class is good approach, but often it is not available for code that uses third party libraries (which is common case for production code unlike in education/sample code). So derive new class if it makes sense, but feel free to use extension methods when they make code more readable.

There are multiple reasons why there are a lot of extension methods:

  • often you can't extend a class to add methods that would make your product code to be more readable. I.e. value types like String or some base classes in hierarchies like Stream.
  • extension methods is valuable way to add methods to interfaces without polluting the interface. LINQ is good example how it produces more readable code.
  • some frameworks recommend to use extension methods in particular cases. I.e. for MVC it is recommended to add extensions to HtmlHelper.

Upvotes: 1

Sachin Kainth
Sachin Kainth

Reputation: 46760

I think all this highlights is the usual difference between theory and practice. In theory we should use them sparingly but in practice we don't. In theory we should do a lot of things that we don't in practice do, in life in general, not only programming.

Upvotes: 2

Related Questions