开发者

.NET "Global" variables; Which one is a better practice?

开发者 https://www.devze.com 2023-02-15 06:49 出处:网络
Which one should I use? This: public class MyClass { static string m_MyString = \"My string here\"; public static string 开发者_StackOverflowMyString { get { return m_MyString; } }

Which one should I use?

This:

public class MyClass
{
    static string m_MyString = "My string here";
    public static string 开发者_StackOverflowMyString { get { return m_MyString; } }
}

Or this:

public class MyClass
{
    public static readonly string MyString = "My string here";
}


Use the latter, but with a slight modification:

public static class CommonStrings
{
    public const string MyString = "My string here";
}

On another note, 'global' variables have the stigma of being bad practice - however, in this instance a common container for reusable strings is preferable in my opinion; I wouldn't go around making everything you want to reuse publicly accessible, though, particularly if they're not constant.


If you only need to share such strings in your code, I would suggest you to use resources (resx) instead of reinventing the wheel.

Just create a resource and use it.

In the other hand, if you're looking to share common values, I would suggest you to create something like an environment class, like one found in .NET class library called "CustomEnvironment" (change "Custom" with any identifier that would be reasonable for you).

This "environment" provides access to common resources, platform-specific information and methods that would format, convert or accomodate any data or object to the platform.

You should avoid "global variables" way of doing things, and classes that are breaking any chance of a good OOP graph. Because "classes of constants" are a good shortcut for quickly doing things, but how you can answer to the question of "what this class represents in your object model?":

  • It's a group of variables: wrong, this isn't a class, because these variables (or constants, or properties) have no meaning: you could implement them in this class or in any other one.

  • It's a class of tools: "do all" tool classes that would move all "common" properties and behaviors to a class so all others are accessing to it for some critical operations isn't OOP, it's just grouping.

  • It's a set of resources. Ok, you don't need to reinvent the wheel, just use out-of-the-box resource-managing facilities in the Microsoft .NET class library.

  • It's just an example. Bad example ;)

So, just answering the question, accepting "global variables" are a bad approach in OOP, you should use constants if the exposed value isn't calculated or it doesn't need any processing, so, you can define it and it'll be assigned during class' construction time.

Perhaps you need to calculate or process something before setting such "constant": you'll use a readonly field, but, for me - just an opinion -, I'll suggest you to use a property in order to expose its value (maybe you won't need such readonly field, because sometimes you'll prefer to calculate/process each time code access to the property, but if you always do it with properties, you can encapsulate this implementation detail).


I would say neither. Rhetorical questions to to ask yourself to help with your decision: Where are they used? Can't they be declared private in the class that uses them? If used by several classes, perhaps those classes can hold a reference to MyClass? Are there any operations associated with the strings that can be put inside the MyClass?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号