Path: utzoo!attcan!uunet!cs.utexas.edu!ut-emx!walt.cc.utexas.edu!jamshid From: jamshid@walt.cc.utexas.edu (Jamshid Afshar) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Keywords: standard,variables Message-ID: <36512@ut-emx.UUCP> Date: 20 Aug 90 18:56:03 GMT References: <2161@runxtsa.runx.oz.au> <425@mole-end.UUCP> <1990Aug19.160307.20972@athena.mit.edu> Sender: news@ut-emx.UUCP Reply-To: jamshid@walt.cc.utexas.edu (Jamshid Afshar) Organization: The University of Texas at Austin, Austin, Texas Lines: 57 In article <1990Aug19.160307.20972@athena.mit.edu> drk@athena.mit.edu (David R Kohr) writes: > >In this discussion of the benefits and drawbacks of declaring variables >at their point of use in a block in C++, rather than at the beginning >of a block as is required in C, I don't recall anyone making the >following point: you can't jump (using "goto") past a declaration with an >initializer unless that declaration is within a block nested within the >current block. (This is according to Stroustrup, _The_C++_Programming_ > >There are those of us who occassionally use "goto" for efficiency >reasons (or even to make the code clearer). It seems awkward and >error-prone to me to have to consciously avoid jumping over declarations >with initializers each time one uses a "goto", and to have to check >that each declaration with an initializer is not in a code segment which >can possibly be skipped over by a "goto". Such awkwardness does not >lend itself to making code more maintainable. [text deleted] I hope this isn't starting a goto debate, but... If the program is jumping into the middle of code that has variables (even if they are not used) active that have not been initialized, the program is _already_ not very maintainable (IMHO). Don't get me wrong, I'm not an anti- goto fanatic. I am using them in a part of my code (at least until C++ gets exception handling) because a non-goto version of the function would look like a hack. I have found the opposite to be true: localizing the scope and delaying the definition of a local variable makes the code much more maintainable. The reason is, if I'm goto'ing _back_ in the code, it destroys any variables decalared after the label I'm jumping to. This way, I don't have any old stuff hanging around. If I jump forward, I don't want to jump somewhere unless all active variables at that place have been initialized. Basicly, I believe if goto's are being used to improve program clarity, delaying local variables declaration until point of use (which may be necessary if they are being initialized) and localizing the scope of variables as much as possible make the use of goto's safer and more understandable to a person reading your code. Besides, the argument that "it makes a program with goto's hard to maintain", seems a little like an oxymoron or something. >of an uninitialized variable. However, compilers can easily detect >instances of the use of uninitialized variables, while detecting the "goto" >error requires a more extensive analysis of the code's flow-of-control. I'm not too sure what you're saying here... Compilers already give an error if you goto past an object declaration. I don't really understand why it's easier to detect uninitialized variables as opposed to skipping variable declarations. Let me know if I misinterpreted what you said. PS: I was really thrilled when I discovered C++ goto's take care of calling destructors. setjmp/longjmp don't, do they? Anybody know when exception handling and templates will be implemented (esp. on PC's)? Will the ANSI C++ standard have them? Finally, does everybody know about comp.std.c++? It's really quiet. --Jamshid Afshar --jamshid@ccwf.cc.utexas.edu