Utility classes.. Good or Bad?

前端 未结 8 671
暗喜
暗喜 2020-12-24 15:19

I have been reading that creating dependencies by using static classes/singletons in code, is bad form, and creates problems ie. tight coupling, and unit testing.

I

相关标签:
8条回答
  • 2020-12-24 16:20

    I agree with some of the other responses here that it is the classic singleton which maintains a single instance of a stateful object which is to be avoided and not necessarily utility classes with no state that are evil. I also agree with Reed, that if at all possible, put these utility methods in a class where it makes sense to do so and where one would logically suspect such methods would reside. I would add, that often these static utility methods might be good candidates for extension methods.

    0 讨论(0)
  • 2020-12-24 16:23

    Utility classes are ok..... as long as they don't violate design principles. Use them as happily as you'd use the core framework classes.

    The classes should be well named and logical. Really they aren't so much "utility" but part of an emerging framwework that the native classes don't provide.

    Using things like Extension methods can be useful as well to align functionality onto the "right" class. BUT, they can be a cause of some confusion as the extensions aren't packaged with the class they extend usually, which is not ideal, but, still, can be very useful and produce cleaner code.

    0 讨论(0)
提交回复
热议问题