Why use GlobalClass? What are they for?

Why use GlobalClass? What are they for? I inherited some code (shown below) and as far as I can see there is no reason why this is necessary for strUserName. What is it all for?

public static string strUserName
    {
        get { return m_globalVar; }
        set { m_globalVar = value; }
    }

      

Used later:

GlobalClass.strUserName

      

thank

0


source to share


5 answers


You get all global state errors and no direct access to the yucky variable.

If you are going to do this, then your coder has done it pretty well. He / she probably thought (correctly) that they could freely replace the implementation later.



In general, this is considered a bad idea, since it makes it more difficult to test the system as a whole, the more global symbols there are.

My 2 cents.

+1


source


If you want to use a static member , use it like ClassName.MemberName

. If the code snippet is in the same class as your member (in this example, you are encoding the GlobalClass member and using strUserName), you can omit the class name. Otherwise, it is necessary because the compiler will not know which class you are referring to.



0


source


This is a common approach when working with Context in ASP.Net; however, an implementation will never use a single variable. So if it's a web application, I could see this approach being used to indicate who the current user is (although there are better ways to do this).

I'm using the simillar approach where I have a MembershipService.CurrentUser property that then pulls the user out of the SessionState or LogicalCallContext (if it's a web app or client app).

But in these cases they are not global as they are narrowly constrained (e.g. http session state).

In one case where I used a global like that, it would be if I had some data that is static and never changes, and is loaded from the DB (and not enough data to justify storing it in the cache). You can just store it in a static variable so you don't go back to the DB.

One side note is why the developer used Hungarian notation to name "Properties"? even when there was no intelligentsia and all the goodness that our IDEs provide, we never used Hungarian notation in Properties.

0


source


@Jayne, @Josh, it's hard to tell, but the code in the question could also be a static accessory to a static field - slightly different from @Josh's static helper example (where you use instance or context variables in your helper).

Static helper methods are a good way to have a convenient abstract set of functions without apathy. However, in this example, there is a possibility that the global variable will be dependent on state - the Law of Demeter tells us that you should only play with the state that you own or are given, for example. by parameters.

http://www.c2.com/cgi/wiki?LawOfDemeter

Given the rules, sometimes there are times when you need to break them. You should trade the risk of using global state (primarily the risk of creating state / concurrency errors) and the need to use global variables.

0


source


Ok, if you want some of the data to be available to any other class running in jvm, then Global class is the way to go.

There are only two small problems:

One. The implication shown is not thread safe. The set method ... of any global class must be marked critical or wrapped in a mutex. Even in the niave example above, consider what happens if two threads run at the same time: set ("Joe") and set ("Frederick") might result in "Joederick" or "Fre" or some other permutation.

Two. It doesn't scale well. "Global" refers to one jvm. A more complex runtime like Jboss can run multiple interacting jvms messages. So the Global User ID can be "Joe" or "Frederick" depending on which jvm is scheduled for your EJB.

0


source







All Articles