#C coding style
11 messages in this thread
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
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
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
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!!!
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?
Nope, it was the fact that you're braces aren't on a line all by themselves 🙂
Dave @OTG
<Rolling on the floor of the computer room, shaking with laughter!>
Rick
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
Thanks. I think I can remember the convention – it sounds straightforward.
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
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