Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!voder!pyramid!kenj From: kenj@pyramid.pyramid.com (Ken McDonell) Newsgroups: comp.lang.c Subject: Re: An Ethics Question Keywords: global variables Message-ID: <65627@pyramid.pyramid.com> Date: 7 Apr 89 11:09:30 GMT References: <1819@uop.edu> Reply-To: kenj@pyramid.pyramid.com (Ken McDonell) Organization: Pyramid Technology Corp., Mountain View, CA Lines: 68 In article <1819@uop.edu> jeff@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@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