Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Summary: Declare at use--a GOOD idea Keywords: standard,variables Message-ID: <425@mole-end.UUCP> Date: 17 Aug 90 03:36:43 GMT References: <2161@runxtsa.runx.oz.au> Organization: mole-end--private system. admin: mole-end!newtnews Lines: 78 > 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: Let me add my voice to those who have have supported this standard (which ought to be a guideline rather than a standard.) > o The declarations clutter the algorithm. The declarations usually look just like an assignment with the type name to the left of the assignment expression. In the case of types which must be initialized with multiple constructor arguments, you probably cannot declare the variable ahead of time. > 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 ? I worked for several years on a medium-sized (150 000) project that had gone through many years of maintenance and enhancement, including a port from assembler into C. One of the things that kept the code clear and reasonably safe between the occasional rewrites of various parts of the code (which usually occurred when developers ganged up on management and testified that they couldn't review the code well enough otherwise) was keeping all variables in the smallest scope possible. It's done, it works, and it saves a great deal of time in debugging and code review. It also ensures that someone won't accidently use a variable that was used to hold an intermediate value in some small computation when they want to use one of the variables that describes the environment in which the function is operating. Reducing the scope of variables to the minimum necessary reduces the number of needless symbols in scope at any given time; if functions exceed about 16 lines, this starts to make a big difference in safety during maintenance and enhancement. It helps to show how the `long- range' communications represented by variables are tied to the `flow-of- control' (Governs-Invokes) structure of the program. And yes, we had functions that were several hundred lines long and that would have been very difficult to split. (In C++, with class scope as an instance record, it would have been easier.) These functions typically broke down into five or six more or less linear steps, each of which was fairly complicated and had a number of special rules that were invoked if certain conditions occurred in previous steps. Being able to declare variables at the point where their initial values became available would have made this code much safer. So would consts. (Only about a dozen functions were this long; most were between ten and forty lines.) > 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. I found otherwise, both on small projects and on that largish piece of very- real-world code. (It's running in several tens of thousands of copies of a business PBX.) Adopting this style improved the review process; the combination of an informal (but conscientious) review process and code style improvements such as this reduced the number of trouble reports that came back as new bugs by probably a factor of four. > 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 find otherwise IF the code starts out highly readable. This means (among other things) that related computations are grouped and set aside from other computations by white space. If your code reads like good prose, in which each paragraph develops one idea in a more or less linear way, with each computation using the results just computed and providing results that are used immediately thereafter, the inserted declarations (which are usually just type names) will state clearly that this is a new datum holding just this value, not the revision of some previous value. That the datum exists just to hold this value can be reinforced by making it a const . -- (This man's opinions are his own.) From mole-end Mark Terribile