If a “Utilities” class is evil, where do I put my generic code? [closed]

岁酱吖の 提交于 2019-11-28 04:16:49

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

Generally, Utils and Helper type classes should be avoided. More often than not, you will find that what you may think is a utility method, is actually a rather specific method that probably belongs in a class of its own (just like you say). However, there will be domain specific cases where Util-like classes (classes which group related useful methods) are valid entities.

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

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

Samples from .NET Framework

  • System.Collections.Specialized.CollectionsUtil
  • System.Net.WebUtility
  • Check Microsoft's source code for .NET Framework. You will find numerous internal utility classes.

You should put it into a 'ListUtilities' or PointListUtilities class, of course. Then you aren't breaking the single responsibility principle, which is the primary problem with a catch-all 'Utilities' class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!