Jaa


Make all your utility methods 'Extension methods'

As I had previously said I love extension methods and have started using them at multiple places like converting enums to strings. The CSharp 3.0 specification clearly calls out the following in red

Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible.

This piece of advice is exactly like being advised not to eat junk food. Doing it makes one happy and lazy irrespective of the fact that he might get obese and in bad shape. So I'm just going to ignore this advice and convert a lot of my utility methods into extension methods.

Why? 'cause you can define an extension method and use it either as method(object); or object.method(). Sometimes it just feels weirdly cool to be able to do that. Let's take an example:

I had a utility method to look up an object in a table. The utility method took a name (string) as argument and returned an object from the table. So I converted the code to extension method like

 public static MyClass Find(this string name){     MyClass mc;    //look up the table;    return mc;}

Now I can either use either of the following

 MyClass mc;mc = Find("abhinaba");mc = "abhinaba".Find(); 

The second option (in bold) is uber-cool. Just the fact that you can call arbitrary function on a sealed class like string makes you feel happy. It's even better if you can sneak this into code which a non-C# 3.0 aware coder is trying to maintain :)

Now since I have not-followed the doctor's advice of avoiding junk food, I feel happy and contented. I tend to ignore that I have difficulty going up stairs and frequently have numbness in my left hand. I think every thing is fine and all the programmers that maintain my code bless me everyday for writing such cool code. It's there blessing that'll ensure me a long prosperous life.

Comments

  • Anonymous
    July 26, 2006
    What if you add a reference to an assembly which contains MyOtherClass, which also defines a Find(this string name) method?

    Sifting through hundreds of ambiguous match compiler errors to fix this "uber-cool" feature will be bad enough, but try explaining to your boss why it took several hours of non-productive work to add the new reference!
  • Anonymous
    July 26, 2006
    The comment has been removed
  • Anonymous
    July 26, 2006
    The comment has been removed
  • Anonymous
    July 27, 2006
    aahh! din't realize you can have only underscores as variable names. Now am converting all my code to the new convention :-)

    my new code goes like ...
    for(int _ = 0; _ < selectedRows.Length; ++)
    {
    IDataRecord __ = query.Provider[selectedRows[
    ]];
    ___ = Convert.ToString([ArgoQueryKey.StrategyId.ToString()]);
    if(!allUniqueStratIds.Contains(
    ))
    {
    allUniqueStratIds.Add(
    __);
    }
    }