开发者

Global access vs. local variables

I have two objects that I will be mainly use inside of single class. I will initialize them at the beginning and use them throughout the life of the program. Now, my question is that if I should just create them as global variables and access them anywhere in the code (in side of single class) or I should create them as local variables and pass them as parameters to other functions. I just want to see what would be the best programming practice.

I am using C#.

开发者_运维问答

Thanks.


In general you should avoid global variables. If it will be practical, I recommend keeping them as locals and passing them as parameters to your functions.

As Josh pointed out, if these variables are only used inside a single instance of the class, then you should just make them private (or protected) members of that class and be done with it. Of course, then they could only be passed in as parameters to other methods with the same access level (IE, private).

Alternatively, you may consider using the Singleton Design Pattern, which is slightly cleaner (and preferable) to using globals.


If the scope of the objects is the lifetime of the class they are instantiated in, then they should be private member variables.

If they do not maintain state themselves, then you should make them static classes.

You should still pass them around as variables, or at least create property accessors to get at the backing field. This way you can change implementation details without blowing up your code.

SOLID design principles are a good place to start when thinking about these things.


I have two objects that I will be mainly use inside of single class. I will initialize them at the beginning and use them throughout the life of the program.

This sounds like a perfect time to use a private static readonly variable. These can be initialized in their declaration, or you can make a static constructor to initialize them.

The fact that you are only referencing these objects within a single class is key point. There are other better ways to do things if these objects are ever needed outside of the single class.


If the objects will be the same for every instance of the class then

static const double PI = 3.14158;


You should generally use accessor methods (e.g. getters and setters) and keep your internal variables private. This way the rest of your code, outside of your class, is not dependent on your actual variables.

See this tutorial.


If your class is dependent on these 2 objects then they should probably be members on the class itself. Something like this (where A is the class you are talking about and B is one of the objects you initialize:

public class A
{
    private B _b;

    public A(B b)
    {
        _b = b;
    }

    public void DoSomething()
    {
        //do something with _b;
    }

    private void DoSomethingElse()
    {
        //do something else with _b;
    }
}

In this example A is dependent on B (so you pass your instance of B into A's constructor or through some Dependency Injection framework). It wouldn't make a lot of sense for every method on class A to need a parameter of type B to be passed to it.


I think in this case you should ask what makes more sense. Is there some kind of relationship between the 2 objects and the new class. Also, how often are they used in the class.

Generally, If only a couple of methods use the objects, pass them around otherwise, instantiate them as class level variables (possibly using private static readonly as Jefferey suggests) and use them in the class. Making the code more readable should be your goal here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜