If the Utility class is evil, where can I put my generic code?

I usually live by the rule that global variables / functions are evil and that every piece of code must live in the class to which it belongs.

This is a very simple rule to follow, and I believe that so far I have never encountered a problem with this rule.

Today, however, I need to add a function to my assembly, and not to a specific class. That is, almost all my classes can use this particular function.

Where should I put this function (+1 overload)?

If I put it in the Utilities class, I feel dirty. If I bind it to a semi-connected class and let other classes call it directly, I feel worse.

This piece of code basically drops IList<PointF> into the normalized list. I feel right now that adding it as an extension method to IList<PointF> might be the best choice ...

+52
c # oop global-variables utility-method
Jul 27 '10 at 0:26
source share
3 answers

If this is an operation in IList<PointF> , then it must be an extension method on IList<PointF> .

In general, the Utils and Helper class classes should be avoided. Most often, you will find that what you think is a utility method is actually a rather specific method, which probably belongs to your own class (just like you say). However, there will be domain-specific cases where Util classes (classes that group related useful methods) are real entities.

+26
Jul 27 '10 at 0:30
source share

There is nothing wrong with global variables and methods. You use them all the time. Structure likes to call them "static" classes or "static" methods.

I rarely need it, but I usually add an internal static Util class in the namespace that the method / variable is required for C # and the module for VB.NET.

Samples from the .NET Framework

  • System.Collections.Specialized.CollectionsUtil
  • System.Net.WebUtility
  • Check out the Microsoft source code for the .NET Framework. You will find many internal utility classes.
+9
Jul 27 '10 at 0:41
source share

You must put it in the ListUtilities or PointListUtilities , of course. Then you do not violate the principle of single responsibility, which is the main problem for the entire class "Utilities".

+4
Jul 27 '10 at 0:29
source share



All Articles