Messages 1-10 from thread Next From: Nasser Abbasi (nasser@apldbio.com) Subject: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++, comp.lang.c, comp.lang.java Date: 1996/12/28 fellow programmers: I really don't not mean to start some religious topic here, but this is something I always wanted to talk about. I really find it very hard to understand why some C/C++ programmers insist in writing code where they put the '{' at the end of the line, making it not aligned with the corresponding '}' and they put the 'else' not under the 'if' ? This style drives me nuts. My eyes gets tired trying to search for the '{' and to see where the other '}' it is paired with, the logic becomes hard to understand, especially when there are a number of 'if' 'else' coupled with 'switch' and 'while' and who knows what else. just look at this piece of code (this is some random function I just picked up from a public BSD telnet source code): ---------------------------------------------------- int ring_full_consecutive(ring) Ring *ring; { if ((ring->mark == 0) || (ring->mark == ring->consume)) { if ((ring->supply < ring->consume) || ring_full(ring)) { return ring_subtract(ring, ring->top, ring->consume); } else { return ring_subtract(ring, ring->supply, ring->consume); } } else { if (ring->mark < ring->consume) { return ring_subtract(ring, ring->top, ring->consume); } else { /* Else, distance to mark */ return ring_subtract(ring, ring->mark, ring->consume); } } } ------------------------------------------------------ Now, I don't care how good a programmer is who wrote the above, but it is just BAD style. compare with this, I rewrite the above clearly: Read the rest of this message... (129 more lines) From: Rev. Ralph J. Gable (rgable@mts.net) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/28 Nasser Abbasi wrote: > > fellow programmers: > > I really don't not mean to start some religious topic here, but > this is something I always wanted to talk about. . . . I am in absolute FULL agreement with you. I have wondered the same thing for a long time myself. I have seen the "bad" style in all the books I own and have even been sucked unto using that style, but never liked it for all the same reasons. You have convinced me to repent and rewrite! I cheer you on. Ralph From: Dennis Weldy (dmweldy@ingr.com) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/28 OK, I guess its up to me. Personally, I prefer the style you dislike. :-) Instead of searching for the opening brace, search for the logical construct if( foo == bar) { // do stuff } else { // do other stuff } I see the if nicely aligned with the }, which aligns nicely with the else, which aligns nicely with the last }. Personally, I've no difficulty with it. What "others" besides the Rev Ralph J Gable, agree? :-) I would think that, if it is scattered throughout books and etc that a) there are folks (authors, perhaps others) who do not find it any more or less difficult b) the seemingly minor advantage of fewer lines becomes real important when putting lots of code examples in a book, where more lines increases the page count, which increases production costs, which increases the cost of the book. Sides, think how many more trees would be saved if you used the more compact format? ;-) I think we'll all agree that no whitespace and everything on one line is a bad style. :-) Otherwise I think its just a matter of personal taste. Unless of course your company has formatting standards in which case you follow those since the code you write for them is their code. What I would find annoying is inconsistency if(a == b) { // stuff } else ir (a == c) { // stuff } else if(a == d) { // stuff } else // other stuff Read the rest of this message... (194 more lines) From: Dave Sieber (dsieber@terminal-impact.com) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/28 Dennis Weldy wrote: > > OK, I guess its up to me. > > Personally, I prefer the style you dislike. :-) I agree with you, but as we all know, it's a matter of personal style. I have my own set of likes and dislikes, and I dislike the extra "blank" lines that result from putting a brace on a line by itself. I don't have a problem seeing the structure of the code when it's clearly written code in the first place (unclear code is always unclear, not matter how it's formatted). I learned my style from the original K&R book, and I still follow most of it today. It's what works for me. I like it. And, like a lot of people, I find my style "right", and the others "wrong". Other things I dislike: Implying that 'return' is a function call: return (0); Cramming parens against if or while: if( x == 1 ) Padding parens with white space: if ( x == 1 ) Comments that take up way too much space (or are useless ): /* * increment x */ x++; And, lord help me, I will never write code as ugly as that written by Petzold. What I would really like is a very-smart reformatter that can handle these kinds of things. INDENT comes close, but it's too kludgy for my tastes. I program on my own, so I don't give a hoot about somebody else's coding style. In a corporate environment you may not have this freedom. Dave -- From: Russ McClelland (russmc@netbox.com) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1997/01/08 Are you still printing code? If it were more readable on the screen, you wouldn't need to! > Sides, think how many more trees would be saved if you used the more > compact format? ;-) > From: Steve Brecher (steve@brecher.reno.nv.us) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/28 nasser@apldbio.com (Nasser Abbasi) wrote: > I really don't [...] mean to start some religious topic here, but > this is something I always wanted to talk about. When the dead and wounded are being carried from the field, your peaceful intentions will have been forgotten. :-) > I really find it very hard to understand why some C/C++ > programmers insist in writing code where they put the '{' > at the end of the line, making it not aligned with the corresponding > '}' and they put the 'else' not under the 'if' ? > > This style drives me nuts. > > My eyes gets tired trying to search for the '{' and to see where > the other '}' it is paired with, the logic becomes hard to understand, > especially when there are a number of 'if' 'else' coupled with 'switch' > and 'while' and who knows what else. > [...] > Putting habits, and all that asides, lets look at this > logically. How can any one not see that the writing > > if( ) > { > foo(); > } > else > { > boo(); > } > > > is MORE clear and easier to understand than > > if( ) { > foo(); > } else { boo() > } > > ?? Let's suppose the braces are to be retained even though they are not required in this case, as that's a separate issue. I would write: if ( ) { foo(); } else { boo(); } That is, I "hide" the braces and let indentation show the structure. In short, I agree with your point about the alignment of "if" and "else," but not about braces. For quite a few years the text editing tools that I use have made it easy to balance braces. If indentation reveals structure, then braces are superfluous to understanding and become clutter that can be ignored by the reader. -- steve@brecher.reno.nv.us (Steve Brecher) From: Kenneth Warner (warner@iris161.msi.com) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/28 Nasser and everybody else, Right on Bro! The old trailing "{" style of programming comes from 1978 the time of Kernighan and Richie (Bless you both, where ever you are!) when "The C Programming Language" was first published. I still have my '78 edition. Then, programmers only had the only the tty interface to work on, 1 screen with 24 lines of 80 characters. How many of you out there actually worked on a tty interface?? It was an optimization for space utilization. Less white space == more lines of code. Usefull then, irrelevant now on all those nice workstations with resizeable terminal emulators we all work on. BTW: if you're still working on a tty, get a better job! The Nasser's analysis of readability is exactly on the money. Yet many of us still insist that the trailing "{" is "The Way of Coding." HUMBUG! When I have to work on legacy code the first thing I do is reformat it into the newer and better-- if (conditional) { /* code block */ } else { /* code block */ } Now if you will excuse me, I have to go put on my asbestos suit. With love to all, Ken Warner In article nasser@apldbio.com (Nasser Abbasi) writes: > fellow programmers: > > I really don't not mean to start some religious topic here, but > this is something I always wanted to talk about. > > I really find it very hard to understand why some C/C++ > programmers insist in writing code where they put the '{' > at the end of the line, making it not aligned with the corresponding > '}' and they put the 'else' not under the 'if' ? Read the rest of this message... (31 more lines) From: Shawn Willden (swillden@cs.weber.edu) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/30 Kenneth Warner wrote: > When I have to work on legacy code the first thing I do is reformat > it into the newer and better-- > > if (conditional) > { > /* code block */ > } > else > { > /* code block */ > } Thrice-damned unreadable crap-style code, if you ask me! Get those braces over where they belong -- with the code they enclose! if (conditional) { // Code block } else { // Code block } Ahhhh, now that's code that *jumps* right off the screen and into your brain. :-^) Shawn. From: Steve Heller (heller@utdallas.edu) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/31 Shawn Willden wrote: ... >Get those >braces over where they belong -- with the code they enclose! >if (conditional) > { > // Code block > } >else > { > // Code block > } >Ahhhh, now that's code that *jumps* right off the screen and into your >brain. :-^) Yes, that's the clearest way to write it. Steve Heller, author and software engineer http://ourworld.compuserve.com/homepages/steve_heller From: Joseph Maroney (jgmaroney@eee.org) Subject: Re: why do some C/C++ programmers write tihs way? Newsgroups: comp.lang.c++ Date: 1996/12/31 Steve Heller wrote: > > Shawn Willden wrote: > > ... > >Get those > >braces over where they belong -- with the code they enclose! > >if (conditional) > > { > > // Code block > > } > >else > > { > > // Code block > > } > >Ahhhh, now that's code that *jumps* right off the screen and into your > >brain. :-^) > > Yes, that's the clearest way to write it. > > Steve Heller, author and software engineer > http://ourworld.compuserve.com/homepages/steve_heller I agree with Steve Heller. if (conditional) { // Code block } else { // Code block } This way is a lot easier to follow then if (condition) { // code block } else { // code block } It may be tolerable for something like this, but can turn into a night-mare and drive many a programmer to drink in nested if-then-else, switch-case statements etc. Next ©2001 Google