[vox-tech] global variables in C
Bill Kendrick
vox-tech@lists.lugod.org
Fri, 20 Sep 2002 11:14:18 -0700
On Fri, Sep 20, 2002 at 10:34:51AM -0700, Peter Jay Salzman wrote:
>
> 1. name clashing
> 2. readability
These are good reasons.
I'm pretty bad about using globals all over the place, though.
My main reason, however, _is_ for readability.
In _most_ cases, the functions I'm calling need to do things to,
or refer to, the same variables over and over again.
As an example, in my games I open the SDL window surface and name it
"screen".
Rather than having to pass "screen" around to ALL of the functions
(first retrieving it from my "setup()" function), I prefer to keep it
global.
Everyone uses it, it's simpler for me to just leave it 'out there'
for them to use. Sure, _some_ functions won't use it, but it's
a lot less cluttery to do something like:
draw_spaceship();
draw_aliens();
Than it is:
draw_spaceship(screen, spaceship_x, spaceship_y, spaceship_dir,
spaceship_color);
draw_aliens(screen, aliens_array, NUM_ALIENS);
It's not like this 'abstraction' of the functions benefits me.
I'll rarely, if ever, call "draw_spaceship()" and tell it to draw
the spaceship on some OTHER surface, or at some OTHER X/Y coordinate
than where the spaceship actually _is_! ;)
IANACG (I am not a C guru), YMMV (your milage may vary),
UAYOR (use at your own risk), GATAA (geez, aren't these acronyms annoying?)
;)
-bill!