global vars vs: local vars 
Author Message
 global vars vs: local vars

I am writing a small program and I run into some problems
of good style. It is at least a question about information hiding.

The functions all handle with one variable.
Now I can declare this variable as
a static local object in the main-function.
Then it has to be passed to all the called
functions.

The other way would be declaring the variable as a static
variable outside the main-function, wich means some kind
of global. Then the question apears, whether to pass the
variable to the functions (wich is not necessary) or not.

Wich way should I use:
 -local
 -global but passing to the functions
 -global and no passing

hans friedrich steffani
--



0721 / 608-6008



Sat, 22 Jun 1996 02:40:10 GMT  
 global vars vs: local vars

Quote:

>I am writing a small program ... functions all handle with one variable.

>Wich way should I use:
> -local
> -global but passing to the functions
> -global and no passing

I would suggest local.  It depends a lot on what you're doing with the
variable.  Sometimes there are good uses for global variables.  Usually
global variables are just hacks put in to save time.  Small programs are
the place to practice writing large programs, and large prgrams can be
nightmares to maintain if there are lots of global variables.
(Remember, only perfect practice makes perfect.)

I've encountered lots of people in CS who let sloppy work in small
programs build bad habits that carry over into big programs.  At least
you're thinking about the problem enough to ask a question, which means
you're on the right track.

Good luck,
michael j zehr



Sun, 23 Jun 1996 01:29:39 GMT  
 global vars vs: local vars
: I am writing a small program and I run into some problems
: of good style. It is at least a question about information hiding.

: The functions all handle with one variable.
: Now I can declare this variable as
: a static local object in the main-function.
: Then it has to be passed to all the called
: functions.

: The other way would be declaring the variable as a static
: variable outside the main-function, wich means some kind
: of global. Then the question apears, whether to pass the
: variable to the functions (wich is not necessary) or not.

: Wich way should I use:
:  -local
:  -global but passing to the functions
:  -global and no passing

: hans friedrich steffani
: --



: 0721 / 608-6008

An alternative is to provide access to your variable via
globally declared functions.
e.g. SetVariable( "Hi there" );
     var = GetVariable();
These will hide the details of how your variable is stored, treating
it more like an object.

Given that your program is a small one, this approach may be
unneccesarily complicated, but it is well worth using for
larger projects or complex variables.

--
     P A U L        P A U L         P A U L       P A U L         P A U L
     C A R T E R   C A R T E R    C A R T E R    C A R T E R      C A R T E R
      3d signature 3d signature  3d signature     3d signature    3d signature
Focus lines:            |             |



Mon, 24 Jun 1996 20:49:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. global vs local vars

2. Global vs. Local vars and .MAP

3. how to access local and global vars in a function

4. basic question about pointers and local vars

5. VC6 SP3 debugger, local vars not recognized

6. global/static vars inside loops

7. const global vars

8. Global vars versus pointers

9. global/static vars inside loops

10. static global vars and functions

11. global Vars

12. Accessing vars in global and module scope in ANSI C

 

 
Powered by phpBB® Forum Software