global variables coding preference

F

Fernan Bolando

I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

I usually just follow the Linux-Kernel Coding style, But in this case it
seems to be unclear to me.

thanks
,Fernan
 
C

Christian Bau

Fernan Bolando said:
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

I usually just follow the Linux-Kernel Coding style, But in this case it
seems to be unclear to me.

Define a struct

struct TApplication;

then use a global variable

struct TApplication* gApplication;

That's one global variable, and it is enough :)
 
E

Emmanuel Delahaye

Fernan Bolando wrote on 02/08/04 :
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

Why did God create functions with parameters?
I am trying to learn of the best way to check if I am mis-using global
variables.

Hard to say, but yes, they can be avoided in most cases, specially in
reusable code.
How do you guys know if there are to many global variables in
your programs?

There is no quota. You can have a perfectly unreadable and
unmaintenable code with exactly one global pointer to a huge structure.
This is not the point.
I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

Using ADT for reusable objects. (the standard FILE object is a good
example)

Using encapsulation and restriced acces (read-only, accessors etc.) at
application level.
 
M

Michael Wojcik

I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

Not true. It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(void)
{
static struct Globals Globals;
return &Globals;
}


Now any function, in any TU ("file"), that needs access to Global1 or
Global2 need only:

struct Globals *Globals = GetGlobals();
Globals->Global1 = 1;

(Obviously, the definition of struct Globals and the prototype for
GetGlobals should be in a header included in every TU, for this sort
of arrangement.)

If you're of the school that considers static variables "hidden globals",
then dynamically allocate the thing at program startup instead.

There are many other reasonable alternatives to global variables.

--
Michael Wojcik (e-mail address removed)

What is it with this warm, quiet, nauseating bond between them?
-- Rumiko Takahashi, _Maison Ikkoku_, trans. Mari Morimoto, adapt. Gerard
Jones
 
M

Malcolm

Fernan Bolando said:
I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.
The best rule is never have a global. Always use a static (file scope)
varaible instead, with get_ and set_ functions.
The advantage is largely psychological, but psychology is important.

Secondly, you only want the get_ / set_ routine where you can't think of any
other way of doing things. For instance, often it will make sense to do
validity checking in the set_ function and check for integrity in the get_
function. Often you will need to set many globals at the same time (for
instance time might need to be set in the same function as date).

However you do still want to keep the number of static globals down. Usually
a function should take its input and output via parameters, not via globals,
though this isn't always possible.
 
F

Fernan Bolando

Not true. It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(void)
{
static struct Globals Globals;
return &Globals;
}


Now any function, in any TU ("file"), that needs access to Global1 or
Global2 need only:

struct Globals *Globals = GetGlobals();
Globals->Global1 = 1;

(Obviously, the definition of struct Globals and the prototype for
GetGlobals should be in a header included in every TU, for this sort
of arrangement.)

If you're of the school that considers static variables "hidden globals",
then dynamically allocate the thing at program startup instead.

There are many other reasonable alternatives to global variables.

First of all I would like to thank everyone who replied. It now have a
bit more idea on how I should approach a situation that seems to need a
global variable.


,Fernan
 
C

Christian Kandeler

Michael said:
It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(void)
{
static struct Globals Globals;
return &Globals;
}

Why not go the exra step and have get/set functions for all the members?
That way, the global structure need not be accessed directly at all
(outside of these special functions, that is). In fact, the rest of your
program wouldn't even need to know about the struct.


Christian
 
G

Gustavo G. Rondina

Christian Kandeler said:
Why not go the exra step and have get/set functions for all the members?

This is a good choic though it is a very OOP approach.


Regards,
 
M

Michael Wojcik

Why not go the exra step and have get/set functions for all the members?

See the phrase "simplest approach" above, which precludes taking an
extra step.
That way, the global structure need not be accessed directly at all
(outside of these special functions, that is). In fact, the rest of your
program wouldn't even need to know about the struct.

In practice, certainly (though there are other advantages to using a
structure for related variables, or for an "execution context" of
some sort, particularly if it's an incomplete (opaque) type outside
of the module where it's manipulated). I was demonstrating that the
assertion "you have to use global variables in some programs" was
wrong (modulo degenerate specifications which require global
variables explicitly, or prohibit the necessary language constructs,
of course).

--
Michael Wojcik (e-mail address removed)

He smiled and let his gaze fall to hers, so that her cheek began to
glow. Ecstatically she waited until his mouth slowly neared her own.
She knew only one thing: rdoeniadtrgove niardgoverdgovnrdgog.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top