Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Wednesday, July 26, 2006 8:42 PM by abhinaba
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Make all your utility methods 'Extension methods'

Wednesday, July 26, 2006 11:21 AM by richard_deeming
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!

# re: Make all your utility methods 'Extension methods'

Wednesday, July 26, 2006 11:53 AM by abhinaba
More you eat junk food the fatter you get :)

So after shoving in 20 Extension method your intellisense list increases by that amount and so does your problem to move around :)

# re: Make all your utility methods 'Extension methods'

Wednesday, July 26, 2006 1:17 PM by Peter Ritchie
There's been a few conversations on this in the past.  What I get from "extension methods should be used sparingly" is that it's just a hack to wedge new functionality into the system where it would have been impossible to do before.  As I've said in the past, Extension methods seem to be a hack to recover from lack (or removal, considering C++ roots) of nonmember functions.  If nonmember functions weren't good enough for C# why are extension methods?

The drawback of this is, as soon as extension methods are released any geek with no self control will surely use them simply because they are there and go out of their way to make sure they could use them.  Not because it's better to use them; but, because they now can and because they can, they must, and will.

Scary, if you ask me.  Kind of like when folks working in C discover a variable name need only contain underscores--which is when the code base is suddenly inundated with variable names like __ or ___.

# re: Make all your utility methods 'Extension methods'

Thursday, July 27, 2006 12:19 PM by brij
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(___);
}
}

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker