Jayne M
Jayne M

Reputation:

Why use a GlobalClass? What are they for?

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

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

Used later as:

GlobalClass.strUserName

Thanks

Upvotes: 0

Views: 213

Answers (5)

James Anderson
James Anderson

Reputation: 27478

Well if you want a piece of data to be available to any other class running in the jvm then the Global Class is the way to go.

There are only two slight problems;

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

Two. It doesnt scale well. "Global" refers to a single jvm. A more complex runtime environment like Jboss could be runnning several inter communicating jvms. So the global userid could be 'Joe' or 'Frederick' depending on which jvm your EJB is scheduled.

Upvotes: 0

stephbu
stephbu

Reputation: 5091

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

Static Helper methods are a good way to conveniently abstract stateless chunks of functionality. However in the example there is potential for the global variable to be stateful - Demeter's Law guides us that you should only play with state that you own or are given e.g. by parameters.

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

Given the rules there occasional times when it is necessary to break them. You should trade the risk of using global state (primarily the risk of creating state/concurrency bugs) vs. the necessity to use globals.

Upvotes: 0

JoshBerke
JoshBerke

Reputation: 67108

This is a common approach when dealing with Context in ASP.Net; however, the implementation would never use a single variable. So if this is a web app I could see this approach being used to indicate who the current user is (Although there are better ways to do this).

I use a simillar approach where I have a MembershipService.CurrentUser property which then pulls a user out from either SessionState or LogicalCallContext (if its a web or client app).

But in these cases these aren't global as they are scoped within narrow confines (Like the http session state).

One case where I have used a global like this would be if I have some data which is static and never changes, and is loaded from the DB (And there's not enough of the data to justify storing it in a cache). You could just store it in a static variable so you don;t have to go back to the DB.

One a side note why was the developer using Hungarian notation to name Properties? even when there was no intellisense and all the goodness our IDEs provide we never used hungarian notation on Properties.

Upvotes: 0

Allain Lalonde
Allain Lalonde

Reputation: 93418

You get all the bugs of global state and none of the yucky direct variable access.

If you're going to do it, then your coder implemented it pretty well. He/She probably thought (correctly) that they would be free to swap out an implementation later.

Generally it's viewed as a bad idea since it makes it difficult to test the system as a whole the more globals you have in it.

My 2 cents.

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422132

When you want to use a static member of a type, you use it like ClassName.MemberName. If your code snippet is in the same class as the member you're referring (in this example, you're coding in a GlobalClass member, and using strUserName) you can omit the class name. Otherwise, it's required as the compiler wouldn't have any knowledge of what class you're referring to.

Upvotes: 0

Related Questions