Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!apple!voder!procase!roger From: roger@procase.UUCP (Roger H. Scott) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Keywords: standard,variables Message-ID: <183@logo.procase.UUCP> Date: 22 Aug 90 01:40:19 GMT References: <2161@runxtsa.runx.oz.au> Reply-To: roger@procase.UUCP (Roger H. Scott) Organization: proCASE Corporation, Santa Clara, CA Lines: 45 In article <2161@runxtsa.runx.oz.au> edward@runxtsa.runx.oz.au (Edward Birch) writes: >A friend of mine works with a large organization where they have defined a >standard that "variables must be declared where first used". > >I think that this is an absolutely insane standard for the following reasons: > > o The declarations clutter the algorithm. This sounds like a completely subjective, personal opinion. Is it intended to be anything more? > o Most C and C++ programmers are used to seeing variables declared > in one place. I believe that changing this will only add to > maintenance costs and development time. > C has had the ability to declare variables at the start of each > block. How often is it used ? Why isn't it used ? Who cares what "most C programmers" are used to? Most C programmers are used to slowly producing large volumes of unmaintainable code. As far as your "belief", again I ask if this is meant to be anything more than a subjective personal opinion? Could you share your reasononing with us? > o I have found that declaring variables where they are first used > significantly adds to the development time of code. With little > benefit to the over all presentation of the code. Have you really done a controlled experiment where the only significant difference in coding technique pertained to location of declarations? > o I also find that declaring variables at the start of functions > in one spot to significantly adds to the readability of the code. > >I would be extremely interested in feedback. It seems that you must be more interested in "what identifiers are declared within a function" than in "what is the actual lifetime and usage of each identifier". I, for one, and *much* more interested in the latter than the former. Generally speaking, I consider an uninitialized variable declaration to be distracting and counter-useful. It is often the case that the uses of any given variable within a function are all relatively close together, although they may be relatively far from the "top" of the function. In these cases I definitely find it easier to write, understand, and maintain code that declares the variables where they are first used. I will go one step further in this direction and say that I wouldn't mind the ability to un-declare a variable after I am "done" with it so that someone else (or me) looking at the code later doesn't have to scan down to the end of the scope to make sure there isn't some other, later use being made of this oh-so-local variable.