dennisdoomen / CSharpGuidelines

A set of coding guidelines for C# 9.0, design principles and layout rules for improving the overall quality of your code development.
https://www.csharpcodingguidelines.com
Other
746 stars 271 forks source link

Suggestion: static classes cause pain when they have dependencies #135

Closed MCCoolMoDAue closed 5 years ago

MCCoolMoDAue commented 6 years ago

we recently started developing using a design method, which can lead to a ton of static functions and classes. This all sounds good and well until you have dependencies. We now have static classes that depend on other non-static classes. Imagining setting up your application using something like autofac is weird to say the least.

For me this speaks against static classes. Maybe this would be a nice reason / indication why not to use static classes.

The reason of testability is in the document already and is connected to what i suggested, but this might clear things up a bit.

dennisdoomen commented 6 years ago

Or maybe more generally: avoid static mutable state?

MCCoolMoDAue commented 6 years ago

good point! in our case we don't have a mutable state, but we have a dependency that we get from a service locator (I know, bad idea, but that's what we have). In my opinion service locator, singleton or just static members are all very similar. So our static class depends on something else that is static. Having a static mutable state would be just as bad, but slightly different.

long story short: static classes can lead to static dependencies (of some sort, service locator, other static classes, singletons...) also like you said, best avoid static mutable state.

My concrete problem is that you cannot use polymorphism with static classes. Because of that we have a ton of if statements deciding which behavior our (static) dependency should have... I'm rambling...

2018-04-11 9:21 GMT+02:00 Dennis Doomen notifications@github.com:

Or maybe more generally: avoid static mutable state?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dennisdoomen/CSharpGuidelines/issues/135#issuecomment-380353287, or mute the thread https://github.com/notifications/unsubscribe-auth/AAnafXyE5w3He9bI0mVTMJ0ElmTmAMhrks5tna74gaJpZM4TPeL5 .

bkoelman commented 6 years ago

Related rules:

I'd be happy to help this issue forward. Is there any concrete work that needs to be done to get this closed?

dennisdoomen commented 5 years ago

Closing as there was no response for over a year now.