Excessive use of extension methods is bad for the same reason that excessive use of overloaded operators is bad. Let me explain.
Let's look at the operator overloading example first. When you see the code:
var result = myFoo + myBar;
you would like to hope that myFoo
and myBar
are numeric types, and the operator +
is performing addition. This is logical, intuitive, easy to understand. But once operator overloading enters the picture, you can no longer be sure of what myFoo + myBar
is actually doing - the + operator could be overloaded to mean anything. You can't just read the code and figure out what's happening without having to read all of the code underlying the types involved in the expression. Now, operator +
is already overloaded for types like String
or DateTime
- however, there is an intuitive interpretation to what addition means in those cases. More importantly, in those common cases it adds a lot of expressive power to the code. So it's worth the potential confusion.
So what does all this have to do with extension methods? Well, extension methods introduce a similar situation. Without extension methods, when you see:
var result = myFoo.DoSomething();
you can assume that DoSomething()
is either a method of myFoo
or one of it's base classes. This is simple, easy to understand, intuitive even. But with extension methods, DoSomething()
could be defined anywhere - and worse, the definition depends on the set of using
statements in the code file and bring in potentially many classes into the mix, any one of which could host the implementation of DoSomething()
.
Now don't get me wrong. Both operator overloading and extension methods are useful and powerful language features. But remember - with great power comes great responsibility. You should use these features when they improve the clarity or capability of the implementation. If you start using them indiscriminately, they will add confusion, complexity, and possibly defects to what you are trying to create.