An Ethics Question

Ken McDonell kenj at pyramid.pyramid.com
Fri Apr 7 21:09:30 AEST 1989


In article <1819 at uop.edu> jeff at uop.edu (Jeff Ferguson) writes:
>
>	Here's a pretty basic ethics question: how do the mighty C
>programmers in netland have to say about global variables?
[ example deleted ]

Whenever there is a choice (and for real examples this is not always
the case, e.g. you probably don't want to cart around global flags
such as "debug on or off?" via arguments to every procedure), local
variables are generally preferred.  Because ...

1. Better software engineering (a la information hiding and object
   orientied philosophies)

2. Better performance for scalar variables (giving the compiler a
   chance at local register allocation to avoid memory references
   is typically a bigger gain than any additional overhead on the
   procedure call/return).

1. is always a truism.  2. depends on other coding style issues,
data structure details and usage dynamics.

>for example, the following two pieces of (idiotically simple) code:
>
>/********* Example 1 **********/
>
>FILE *fp;
>
>main()
>{
>	/* code */
>}
>
>readfile()
>{
>	/* code */
>}
>
>/******** End Of Example 1 *********/
>
>/******** Example 2 **********/
>
>main()
>{
>	FILE *fp;
>
>	/* code */
>}
>
>readfile(fp)
>{
>	/* code */
>}
>
>/******* End Of Example 2 ********/
>
>Global variables or passed parameters?  I'm anxiously awaiting the pros
>and cons of this issue.  Thank you.
>
>
>--
>Jeff Ferguson		       ...!{ucbvax|lll-crg}!ucdavis!uop.edu!jeff
>Computer Science Department    ...!cepu!retix!uop.edu!jeff
>University of the Pacific      jeff at uop.edu	(209) 944 - 7105
>
>"My father to the left of me, my mother to the right,
> Like everyone else they're pointing, but nowhere feels quite right"
>					-- Genesis



More information about the Comp.lang.c mailing list