Global variables v Settings in C #

I read in various places that the presence of variables with a global scope, i.e. an open static class with static members is considered contrary to the philosophy of OO and is not a good design. (For example, I saw comments line by line: “If you use the global, you are not doing it right.” Or words about it.)

But, if you use the settings engine provided by Visual Studio, for example. "Settings.Default.MySetting", etc., It is available worldwide throughout the application, since it is different from using an open static class?

In addition, the same results can be achieved using a singleton object, but this also provokes at least different opinions.

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

I am particularly interested in people's opinions about using the "Settings" functionality. Is this a good OO design?

Thanks for any comments.

+6
c # global-variables settings winforms
source share
5 answers

Static methods and other participants are not bad practices in and of themselves. It's just that people who are less familiar with the concepts of OO tend to clog their code with static methods, properties, and fields everywhere, not understanding what the consequences are.

In general, for things like configuration settings, helper classes and utilities, abstract factories, singletones, etc. that have static elements, they are perfectly acceptable.

+14
source share

In C #, it will not be easy for you to go against a good OO design, because you cannot get away from OO. This is not like C ++, where you can mix and match structured OO programming - an area in which similar arguments often arise. Static class members are OO. Microsoft is also creating it because code generation creates an OO encapsulation for them, or at least a “container of objects” around them. Thus, they are never global variables because they do not exist in C # - they are just static members on classes - there is nothing non-OO there.

If the argument is about singleton vs static member, then it exposes one OO argument against another OO argument.

Then there is always a philosophical point of view and a practical point of view. In most areas, an ideal philosophical point of view is not realized on its own, with the exception of academic studies. The real world needs real solutions, mixed solutions.

+2
source share

An open static class or member is not always a bad idea (even if it's not really OO). Many good OO projects use a public static member for things like Loggers or Settings (as you specify). A good example of how to do this in the OO method is Static Gateway .

+1
source share

Global variables, such as gotos, are something that should be avoided by all newbies, but are extremely useful for an advanced programmer.

I would say that if you do not feel confident, and you have no specific, justified reason why this is a good use, do not use them. OO master before resorting to global variables.

+1
source share

Settings engine ... hmm ...

I look at them mainly as the environment. Like OS or Time, but for the application. They are not “variables,” as you stated during INIT.

However, you can wrap an object around them and access them only through this object, instead of reading them when they are needed at runtime. I have not tested it, but it is likely to erase the performance (or negative, to read them raw, if you are not doing good memory management).

In the end, as applications mature, things like that end up wrapping things around them anyway. My rule, when I start to think: "No, it's too simple, too atomic and will not require an object ...", which is my hint to make it an object.

0
source share

All Articles