8 Replies - 2560 Views - Last Post: 27 October 2008 - 12:08 AM

#1 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Global objects in line with guidelines of OOP?

Post icon  Posted 25 October 2008 - 10:52 AM

My teacher now and then says that you should be very restrictive with global variables since they, according to him, are not recommended in object oriented programming.

Still he uses global variables himself. An example is that he creates objects which he refers to with global variables in windows applications.

With this as a background I wonder:

Are global variables really dissuaded in OOP? I am especially interested in knowing if global declaration and initialization of objects, which my teacher does, is OK.

Is This A Good Question/Topic? 0
  • +

Replies To: Global objects in line with guidelines of OOP?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Global objects in line with guidelines of OOP?

Posted 25 October 2008 - 11:23 AM

There is a continuing debate on the pros and cons of using Global variables. Some say they are flat out bad and you should never use them, some say that they are an acceptable form of storing global values, as long as they are used sparingly. I for one feel that global variables are ok as long as they are used carefully, and not made global to the application.

Class level variables are ok in my opinion, Code Complete has a good section on the pros and cons of using global variables. If you need constant values, think about using a struct or an enum, you can also serialize global values or store it in a global Hashtable. Unfortunately I don't believe there's a one size fits all approach to global variables, and they should be assessed on an application to application basis. I think this is a good topic for discussion, so I'm going to feature this and move it to the Corner Cubical
Was This Post Helpful? 0
  • +
  • -

#3 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Re: Global objects in line with guidelines of OOP?

Posted 25 October 2008 - 02:56 PM

Thanks for the link, PsychoCoder and a good answer. I don't think there is a simple absolute answer either and I am in a middle of an important choice right now which verifies this. So my reason for asking is not only because of the way my teacher is programming, it has some consequences for me too which will be explained below.

I am creating an application right now (a Mastermind game to be more specific) and I'm using private static variables in some classes (for example the board class), accessing these variables with public instance methods. These variables contain data that I need to have available everywhere in my central MainForm class. In this way I can create instances of the classes in the MainForm class but the instances will be using the same static variables through their methods.

An alternative to this would be to change all static variables to instance variables in my classes but then I would be more or less obliged to use the same global (class-level) instances of these classes in my MainForm class.

So right now I have to make a choice between using static variables or global instances. Maybe there are other options than these two though?

This post has been edited by Zeddicus: 25 October 2008 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Global objects in line with guidelines of OOP?

Posted 25 October 2008 - 04:42 PM

Static is, if anything, almost as bad as global. :P Why can't you simply pass the instance around as needed?

The reason global instances are discouraged is the same reason static is discourage, it promotes poor design. I would encourage you to try to avoid both.

If you must have a global instance, a singleton design pattern is commonly used. Essentially, you have a class with a private constructor and a static method to access one, and only one, instance of the class. This allows the entire application to manipulate a common object pool.

More info here.
Was This Post Helpful? 0
  • +
  • -

#5 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Re: Global objects in line with guidelines of OOP?

Posted 26 October 2008 - 03:06 AM

View Postbaavgai, on 25 Oct, 2008 - 04:42 PM, said:

Static is, if anything, almost as bad as global. :P


Hehe, that was what I suspected. The only excuse I have is that I'm very new to programming and started out for real just a couple of months ago. I'm reading a beginner course in programming right now.

View Postbaavgai, on 25 Oct, 2008 - 04:42 PM, said:

Why can't you simply pass the instance around as needed?


Because I am not sure on how to do this in a successful way in my current application. I really would like to know how though. I need to pass instances around (with preserved states) between different event methods in my MainForm class.

View Postbaavgai, on 25 Oct, 2008 - 04:42 PM, said:

The reason global instances are discouraged is the same reason static is discourage, it promotes poor design. I would encourage you to try to avoid both.


Yes, I think it is important to learn how to avoid these from the beginning of your programming career, or else you will build up a bad habit. Maybe you can use global variables in some or a few cases but to use them in every other application (because you don't know anny other way) would not be good at all. I think my teacher has failed in explaining how to avoid them because he uses them himself in many of the test applications he creates in class.

View Postbaavgai, on 25 Oct, 2008 - 04:42 PM, said:

If you must have a global instance, a singleton design pattern is commonly used. Essentially, you have a class with a private constructor and a static method to access one, and only one, instance of the class. This allows the entire application to manipulate a common object pool.

More info here.


Thanks for the info. It may be useful in future applications but I don't think it will be necessary to use it in this one.

This post has been edited by Zeddicus: 26 October 2008 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Global objects in line with guidelines of OOP?

Posted 26 October 2008 - 04:38 AM

View PostZeddicus, on 26 Oct, 2008 - 06:06 AM, said:

I need to pass instances around (with preserved states) between different event methods in my MainForm class.


Quick example.
// fairly useless class, just to show the idea
class State {
	private boolean isLocked = false;
	private int transCount = 0;
	
	public int TransactionCount { get { return transCount; } }
	public void IncrementTransactionCount() { transCount++; }
	public boolean IsLocked { get { return isLocked; } }
	public void Lock() { 
		if (isLocked) { throw new Exception("Already locked, always check first."); }
		isLocked = true;
	}
	public void UnLock() { isLocked = false; }
}

// In our MainForm
State state;
public MainForm() {
	state = new State();
}

private void LaunchForm2() {
	// set some state
	state.Unlock();
	// call a form
	Form2 frm = new Form2();
	// pass the instance with custom public method
	frm.SetState(this.state);
	// let the form do it's thing
	frm.ShowDialog();
	// When the form exits, see if it changed state
	if (state.IsLocked) {
		//  do something
		state.Unlock();
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Re: Global objects in line with guidelines of OOP?

Posted 26 October 2008 - 03:41 PM

baavgai: A short question to clarify things: what do you actually mean with global variables?

Do you consider private variables in a class (like state above) to be global? Confusingly enough my teacher sometimes denotes private instance variables as global. But this cannot be right, right? :)
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Global objects in line with guidelines of OOP?

Posted 26 October 2008 - 06:59 PM

Good question.

All variables have scope. Rule of thumb is to make the scope as narrow as you possibly can. When a variable is at the top level in scope in a class, it can be considered "global". However, it's only global to an instance of that class. You want to have as few of these as possible, only enough to get the job done. Also, they should always be private.

To have something global to the application you enter the land of static. In objects, a variable that is available to the entire object is usually required for some functionality. A static, or class, variable is available to the entire application. This is a major design issue, because when any part the program can manipulate the value, it's very hard to maintain and debug.

Think of this this way. Image you have a project spanning many classes. If you make a change to a class, how many other classes will you also have to change? If your changes are hidden behind the shield of private, the answer is none. All the extra effort of OOP is hard to understand for a small program. It begins to make more sense when projects get bigger.
Was This Post Helpful? 1
  • +
  • -

#9 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Re: Global objects in line with guidelines of OOP?

Posted 27 October 2008 - 12:08 AM

View Postbaavgai, on 26 Oct, 2008 - 06:59 PM, said:

Good question.

All variables have scope. Rule of thumb is to make the scope as narrow as you possibly can. When a variable is at the top level in scope in a class, it can be considered "global". However, it's only global to an instance of that class. You want to have as few of these as possible, only enough to get the job done. Also, they should always be private.

To have something global to the application you enter the land of static. In objects, a variable that is available to the entire object is usually required for some functionality. A static, or class, variable is available to the entire application. This is a major design issue, because when any part the program can manipulate the value, it's very hard to maintain and debug.

Think of this this way. Image you have a project spanning many classes. If you make a change to a class, how many other classes will you also have to change? If your changes are hidden behind the shield of private, the answer is none. All the extra effort of OOP is hard to understand for a small program. It begins to make more sense when projects get bigger.


Really good answer. Thanks a lot. I am eager to begin with bigger applications so I can understand the benefits of OOP fully.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1