views:

298

answers:

4

I have a large collection of static 'Utility' classes that contain very generic static methods. For example, I have a CollectionUtility class that has useful methods like:

public static void RemoveDuplicates(ICollection collection)... etc

With C# 3.0 I've been converting these to extension methods.

Now, I've heard some talk that in an 'enterprise level' application it is often considered best practice to avoid large libraries of these static classes and methods. I imagine that it could get quite hard to maintain.

A question for those of you who work on large enterprise projects for large companies - do you maintain libraries of utility classes like these or not? What do you?

A: 

Absolutely not. Utility modules over time turn into large collections of cruddy code.

1800 INFORMATION
A: 

I usually only put things like configuration and constants in a singleton or static class, because it will never change and might as well be "global".

mannu
Configuration will never change?!? -1 No soup for you.
David B
I wasn't clear enough, I believe. I mean values that will never change in the applications lifecycle, for example properties to grab values from web.config.
mannu
+1  A: 

You're talking about code that would be shared library stuff. Static methods do have a place in shared libs. Check out System.Linq.Enumerable

I'd follow these guidelines:

  • These aren't static methods by default. They should only be static methods because they are naturally stateless (behavior only depend on parameters). If they aren't naturally stateless, then you should be making a proper class to manage that state.
  • Cover these with Unit Tests. If you don't Unit Test anything else, Unit Test these. This should be very very easy to do. If this isn't easy, this isn't right.

If dependency injection is something you like, you can still have it. Code that relies on a static method could instead be calling a Func(T, U) or an Action that references that static method.

David B
A: 

I've heard some talk that in an 'enterprise level' application it is often considered best practice to avoid large libraries of these static classes and methods. I imagine that it could get quite hard to maintain.

IMHO you should apply things like generics to cut down on the size of your utility methods/libraries, and if you only use a utility method in one place, then it doesn't belong in a shared library, but at the end of the day you're still likely to have quite a lot.

At any rate, this perplexes me. If you didn't put them in a shared library where would you put them? Copy/paste into each project or something daft like that?

Orion Edwards