Utility classes.. Good or Bad?

前端 未结 8 709
暗喜
暗喜 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: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.

提交回复
热议问题