Sign in to follow this  
Sereth

Alternatives to global?

Recommended Posts

Sereth    122
I've seen similar questions posted but I couldn't find one specific to this problem, and I haven't been able to find anything anywhere else. I have a fair amount of experience programming but most of it doesn't cover this sort of thing, because it's all fairly small scale. I programmed a small text-based game in C++ and in my code I created a class for characters. I found myself passing the objects by reference for characters between all of my functions so everything would be accessible from wherever I was. I've been told that global declarations are bad practice but this doesn't seem much better to me, especially for games that are larger scaled than mine. What are the techniques generally used for such things? Thank you for any help beforehand, I really appreciate it (this has been bugging me for a while now).

Share this post


Link to post
Share on other sites
Kylotan    10008
In practice, in large software you rarely need any data to be visible absolutely everywhere. Often you only need it in certain parts, or you only need subsets of the data. Why do you think it "doesn't seem much better"? It's not about saving keystrokes, but about avoiding bugs that can crop up by having a lot of shared state. If you were able to eliminate all shared state, you could write and debug every single function in isolation, making development much easier. Removing globals is a big step in that direction.

Share this post


Link to post
Share on other sites
Andorien    139
Something to also think about is the issue of code reuse. If your functions and classes rely on global variables contained in the program, it'll be difficult to try and reuse those functions for other programs, or even other parts of the same program that might want to work with different data. Passing all the data back and forth might seem repetitive and inefficient, but in reality it keeps your code flexible and prevents it from turning into a huge mess, especially if you need to come back, look at the code again, and try to figure out what all these global variables are.

Share this post


Link to post
Share on other sites
stonemetal    288
Quote:
Original post by Sereth
I found myself passing the objects by reference for characters between all of my functions so everything would be accessible from wherever I was. I've been told that global declarations are bad practice but this doesn't seem much better to me, especially for games that are larger scaled than mine. What are the techniques generally used for such things?


Well from this description there are several things that could have gone wrong with the design. It sounds like you have a leaking class somewhere. Either the player class or whatever holds the player class has methods that best belong with the player class but for some reason aren't. The other alternative is the player class has picked up cruft that really belongs somewhere else, and you are handing the player around to handout the cruft. It could be a perfectly reasonable design, and you are just wanting to decrease coupling. The recent article on event systems is one way to reduce coupling.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Sereth
I've seen similar questions posted but I couldn't find one specific to this problem, and I haven't been able to find anything anywhere else. I have a fair amount of experience programming but most of it doesn't cover this sort of thing, because it's all fairly small scale.

I programmed a small text-based game in C++ and in my code I created a class for characters. I found myself passing the objects by reference for characters between all of my functions so everything would be accessible from wherever I was. I've been told that global declarations are bad practice but this doesn't seem much better to me, especially for games that are larger scaled than mine. What are the techniques generally used for such things?

Thank you for any help beforehand, I really appreciate it (this has been bugging me for a while now).


It's hard for me to describe these things in the abstract. I could try to just dump out all the advice that comes to mind, but lots of it could turn out to be irrelevant.

Here, step into my workshop (i.e. show the code) and let's see what I can do for you. :) You say it's "small"? Should be no problem, then? :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this