Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables v Settings in C#

I have read in various places that having variables with global scope, i.e. a public static class with static members, is considered going against the philosophy of OO, and is not good design. (For example, I have seen comments along the lines of: "If you are using a global, you are not doing it right." Or words to that effect.)

But, if you use the Settings mechanism provided by Visual Studio, e.g. "Settings.Default.MySetting" etc, this is available globally throughout an app, so how does this differ from using a public static class?

Also, the same results can be achieved by using a singleton object, but this also provokes various opinions, to say the least.

Global variables are just SO useful, (VB Module, anyone?), but I'm trying to teach myself how to do this OO malarky properly, so, if global variables smell bad from an OO point of view, what is an alternative?

I'm particularly interested on people's opinions of the use of the 'Settings' functionality. Is this considered good OO design?

Thank you for any comments.

like image 221
Andy Avatar asked Oct 29 '09 18:10

Andy


1 Answers

Static methods and other members aren't bad practice in their own right. It is just that people less familiar with OO concepts tend to litter their code with static methods, properties and fields everywhere, without realising what the consequences are.

Generally, for things like configuration settings, helper and utility classes, abstract factories, singletons etc., having static members is perfectly acceptable.

like image 187
Wim Avatar answered Sep 24 '22 22:09

Wim