CompuServe Thread

#C coding style

11 messages in this thread
#58206From: George BricknerJul 21, 1989 11:21 PM
Speaking of your _A Matter Of Style_ article in the May '89 TransAmi. After digesting the article, I found everything you presented made a lot of sense and I started adopting your techniques in my development. I'm also attempting to "spread the gospel" at work amoungst the newly initiated C programmers. You didn't mention the DO WHILE clauses in the article. I've been using the following style and wondered what your opinion may be: do { statement (); } while (condition); Or do you find the following clearer: do { statement (); } while (condition); After the big Lattice vs Manx debate here, I decided to pick up Lattice 5.0x at AmiExpo next weekend to replace PDC. Geo
#58231From: David HamkinsJul 22, 1989 11:08 AM
Geo, I'm not Ben but let me offer you my advice. Whenever you use braces, place each one on it's own line. This emphasizes the block nature of the statements enclosed by the braces, and makes spotting the missing "}" a heck of a lot easier. Thus, write do { statements ; } while( condition ) ; Doesn't it seem much easier to spot the "}" this way rather than do { statments ; } while( condition ) ; Dave @OTG
#58251From: George BricknerJul 22, 1989 1:50 PM
Well, Dave, that's the kind of opinion I was seeking. I've found that indenting the braces AND the block of code between them makes the block even more obvious. Ok, the "while" goes on a new line … do { statements ; } while (condition); Thanks for your input! Geo
#58688From: Chris GrayJul 24, 1989 9:30 PM
You realize of course that indentation styles are a matter of religion? The ONE CORRECT WAY to write a 'do-while' in C is: do { statements; } while (condition); 1) there is no point in using a separate line for the braces – they are intimitely bound to the statement itself (after all, NOONE does compound statements without braces!) The indentation structure is quite obvious in the above. 2) there is a space between the 'while' and the '(' – this is to make it NOT look anything like a function call. 3) there is no space between the '(' and the condition, etc. Such extra space is wasteful of trees 4) the indentation is 4 characters each. This allows a reasonable number of levels without running past 80 columns In case you hadn't guessed, this is my personal opinion only, and nearly every programmer I have ever met has a different style of doing this!!!
#58689From: Chris GrayJul 24, 1989 9:32 PM
Oh, ick! – it reformatted it for me. Could some kind soul tell this poor CIS beginner how to tell the forum to keep my spacing at the beginning of lines? It did ok for the first bit, but then messed up. Was it the mismatched parentheses?
#58705From: David HamkinsJul 24, 1989 9:53 PM
Nope, it was the fact that you're braces aren't on a line all by themselves 🙂 Dave @OTG
#58796From: Richard Rae/SYSOPJul 25, 1989 12:00 PM
<Rolling on the floor of the computer room, shaking with laughter!> Rick
#58739From: Don Curtis/SYSOPJul 25, 1989 1:30 AM
Chris, The "code" looked fine, but the formatting of the comments after the code was messed up. If you start a line (very first character) with a period, tab or space, CIS will leave that line alone (or the start of it anyway) examples: this line started with a tab this line started with a space this line started with a period Of course if you continue the lines too far, things can get really messed up, these two lines started with tabs, and should be wrapped at least somewhat on your screen. likewise note that if you start a line with a space, it stays but if you start it with a period the period isn't displayed when the text gets displayed as a message. Hopefully, that didn't confuse you. Don
#59032From: Chris GrayJul 26, 1989 9:35 PM
Thanks. I think I can remember the convention – it sounds straightforward.
#58981From: Betty Clay/SYSOPJul 26, 1989 3:17 PM
Chris, If you want to keep your format, put a space or a dot at the beginning of each line you want to hold a certain way. Otherwise, the system pulls everything up to fit the screen of each viewer. Betty
#58871From: George BricknerJul 25, 1989 9:12 PM
Well, Chris, I've noticed that since I've adopted Ben Blish's "style", my C code has become somewhat more readable. I've showed the style to one or two other programmers and they found the style desirable for various reasons. And on the subject of readability: If I'm expected to understand what I wrote a week or two after I wrote it, I intend to use a style that reduces eye-strain (important because I get grouchy when I have a headache). So there. Nyah! (Don'tcha just love these discussions between professionals?) Geo