Over the past couple months, we’ve been playing more and more with C# 3.0, .NET 3.5 and Visual Studio 2008 (man, it’d be nice to get these version numbers synced up). At first I was naively optimistic about the usefulness of extension methods, but in practice, they seem to add no real value and, on occasion, decrease readability.
The C# specifications themselves point out this flaw:
(Interestingly, I couldn’t find the same such warning for VB.NET – maybe I was just looking at the wrong document).
I understand that extension methods were required for LINQ, but I haven’t come across, nor am I able to think of, a situation in which it’ll ever be necessary for me to use. Open classes have been a small stumbling block for Java/.NET programmers doing the switch to Ruby, and I see extension methods as a poor-man’s implementation of those.
In the end, like with anything else, it comes down to responsible and proper use. With or without extension methods, a bad programmer will write un-maintainable code. However, I do wish that the countless blogs and articles covering the new Orcas features, like extension methods, would do more than show-off the syntax and pump out a few examples. Explaining shortcomings, pitfalls and proper usage is more important than shallow examples.
For me though, I’ll stick with my explicit procedural StringUtility class.