Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!unf7!tlvx!sysop From: sysop@tlvx.UUCP (SysOp) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Summary: declaring in blocks, or separately Message-ID: <391@tlvx.UUCP> Date: 19 Aug 90 05:53:31 GMT References: <2161@runxtsa.runx.oz.au> Organization: Temporal Vortex BBS of Jacksonville, Florida Lines: 56 In article , zmls04@trc.amoco.com (Martin L. Smith) writes: > We have just started to move from C to C++ and the declare-at-usage > style still looks a little strange. I've noticed a tendency in my own > code to declare throwaway variables (the index in a loop, say) when > they are used but to group more significant ones in declaration > chunks, which are not always at the start of a function block. > I've noticed at least two drawbacks in this style: > > 1 - It's cowardly and has no really clear principles. Wouldn't the principal be that it's easier to program on-the-fly (er, so to speak)? It's not so bad putting everything at the top if I'm in an editor that easily lets me enter into different windows.... > > 2 - Declaring loop indices at point of use leads to some problems in my > case, at least, because I tend to reuse i, j, etc, and C++ doesn't > like repeated declarations (at least in the first clause of a > for-loop). The second for loop that uses 'i', for example, should > not declare it. If I later delete the first loop then I have to remember > to add the declaration to the new first loop (the old second loop, > that is [are you still there?]) or I get a compile error. This symptom > strikes me as an indication that this style is not the best we could do. I'm just starting to learn C++, and in making very simple programs, I ran into this very problem! Part of me really likes the convenience, but it seems to me that in the 2nd loop, that if I say "for (int loop=0..." and it is already declared, as long as the delarations match (they're both ints), it might as well use the same variable. (Am I missing something?) I mean, it's hard to argue that that would be a casual thing to do, since declaring at first use is casual anyway. It seems a pretty small matter, but as a bug, it bit me enough for me to remember it. On a related topic, in C, you can declare a variable anytime you open a brace ("{"). This seems like a neat idea, since code within braces is more likely to move as a unit. (To get around the problems, I guess you would surround a for loop with a set of braces external to that, then put the declaration, and then the for, but by the time you do all that, it just clutters things up, so why bother?) I guess if it's too messy, you could avoid it, like avoiding the goto, or at least list situations where it's not so messy. > > Anyhow, I too would really like to read more discussion on this issue. > -- > > Martin L. Smith Amoco Research Center > P.O. Box 3385 > zmls04@trc.amoco.com Tulsa, OK 74102 > [zmls04@sc.msc.umn.edu] 918-660-4065 -- Gary Wolfe, SYSOP of the Temporal Vortex BBS ..uflorida!unf7!tlvx!sysop, ..unf7!tlvx!sysop@bikini.cis.ufl.edu