The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,573 other followers

co-worker question: what is better, extension methods or helper classes?

Posted by jpluimers on 2010/09/29

Recently, a co-worker at a client asked me “What is better: extension methods of helper classses?”.

This question is relevant not only in the .NET and Delphi worlds.

My answer was simple, consisting of these 3 points:

  1. If you can extend your class hierarchy in a better way, then do it
  2. If you can’t then be aware .NET extension methods always require a helper class, and
  3. Helper classes in Delphi are richer than extension methods in .NET

Point 3. is to indicate that you need to be aware of what you can or can’t do in the various languages you use.

There is more than just helper classes and extension methods.
Generics add a totally new means of re-use, so consider that as well.
Sometimes carefully crafted interfaces also help a lot in extending an existing class library.

The reason for not doing 1. must be strong: usually some because you cannot alter the class library on which you want to apply your helper/extensions.
Some circumstances:

  • If the class hierarchy you are using is in an external library you cannot modify
  • If the class hierarchy consists of generated source code that might need to be re-generated, and the generated code does not allow for extension (.NET: it does not use partial classes, general: it is generated in a very closed way).
    Some WMI, WSDL, XSD, and other source code generators come to mind.
    Make sure you watch your generated code, because some generators do generate very extendable code.


5 Responses to “co-worker question: what is better, extension methods or helper classes?”

  1. Xepol said

    I seem to recall that in Delphi you could only have 1 class helper per class because it was meant as a D2007 feature kludge – really only meant for the codegear guys to use. If that is correct, and if it is still the case, then class helpers are to be avoided because the results can be unpredictable.

    • jpluimers said

      It works differently; the closes class helper in your scope will be picked.
      So you need to be careful.

      But: since class helpers can inherit from other class helpers, you have a good way of extending stuff.


      • runner said

        For your own code I agree. But then as you pointed out, in such cases you have a better solution.

        In third party libraries this can be tricky. What if the author of the library sudenly uses the helper itself?

        • jpluimers said

          When a 3rd party library starts using class helpers, then you clearly have a new version, so you need to see if your code needs adaption anyway.


  2. Personally — I love class helpers, it’s one of the best features introduced in Delphi since 2004(just my opinion).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: