Advertisement

Global vars

Started by May 31, 2001 03:34 PM
19 comments, last by the darkening 23 years, 8 months ago
I wonder how much global vars are "allowed" for programs. I always thought they are a symbol of bad programming knowledge.
It depends on the language & technique; if you''re writting in C you''re going to have more globals than C++ or Java.

As the program grows in size, it start becoming difficult to remember or recongize the ''rules'' about twiddling global variables. If you use structures & pass more parameters, you don''t lose sight of what it was for so easily.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
I use whatever I think is best for any given situation. Usually I use globals. I''m sure I could pass variables to everything but it would be more of a headache than it''s worth.

Ben
http://therabbithole.redback.inficad.com
I prefer to encapsulate all my global vars into a single struct
g_sVars

This does not include singletons or global class instances. Just makes for neater code.

InFerN0

Not all who wander are lost...
InFerN0Not all who wander are lost...
Like Magmai, I use a global structure for my globals. Then when I use one of the variables in it, I can type
globals.n
and the auto-complete shows me a list of all my n* globals.

This also keeps their declarations all in the same location, prevents variable name collision, etc.

One reason globals are good is to keep your function call lists short. If you only have to pass one or two variables, the compiler can use the registry. If you start having 4 or more variables, then it has to push data onto the stack and your speed suffers.

There is no hard and fast rule here. In general, yes, globals make for messy code, so make sure you apply a structure and a pattern to them. Just realize that they can also help with speed of communication between function calls, libraries, objects, etc.

Any large program will have a large number of globals, there''s generally no way around it. And heap sizes these days don''t have too much problem with volumes of data.
Dustin
if speed is in issue using just plain globals is the fastest way

putting it in a struct slows down stuff cuz the compiler has to get the pointer and add the value''s offset....

passing parameters in function is a bit slow too
cuz it works through the stack
so you have to push pop variables

Arkon
[QSoft Systems]
Advertisement
Sometimes there''s just no way around globals. It''s just not worth passing it through 4 different functions on the way to that 5th function. That''s when globals come in. A few globals here and there never hurt anybody, but too many can become confusing. Globals are also the fastest >

Don''t resist RoastBeef - You know you''re hungry
Don't resist RoastBeef - You know you're hungry
Lets suppose I have my program in 2 seperate files. Lets say I have a function in one of the files, which needs to use a variable that is in the other file. Is there any way to make a variable accessible between files without making it global or putting it in a global struct/class?
Say you declare your variable in file1.cpp and wish to use it in file2.cpp without worrying about headers and "ownership disputes", simply declare the variable (again) in file2.cpp as extern.
  extern int variable;   // this variable is owned by another file  

Some C++ guys use only class members and declare most of them private and think they code in a super object-orientated way. I think that is ridiculous and slow down your tempo. Let say u have a int variabel called x in a class. Why should I make it private and change it with two member functions (commonly called get/set something) instead of declaring it public?

I say, use global variables a lot!



Zeblar Nagrim, Lord of Chaos

This topic is closed to new replies.

Advertisement