how has C bitten you? (Really, style)

Chris Shaw cdshaw at watmum.UUCP
Thu Aug 22 10:41:39 AEST 1985


In article <372 at ttrdc.UUCP> levy at ttrdc.UUCP (Daniel R. Levy) writes:
>In article <389 at phri.UUCP>, roy at phri.UUCP (Roy Smith) writes:
>>	Here's one that just got me:
>>		if (sv > score);   <----- note extraneous semi-colon
>>			score = sv;
>
>Sounds like a question of style hiding function.  Why not stick to something
>like
>		if (sv > score) score = sv;
>?
>|       dan levy 

...because 

if(sv>score||this==that+the_other||fopen("crap","r"))save=the+whales+fur+christ++;

is the kind of statement where bugs really happen. Can you seriously spend less
than two seconds reading that to comprehend what's going on ? If you answered
yes, how about this (more important) question: Can you read a whole FILE of
this kind of crap and then be able to find a variable at will ?

I doubt it. I can think of more straightforward ways of producing code, some
of which include programming while awake, so that the errors like the
one in the original posting don't happen. Others include using a self-consistent
style, which Mr Levy's is not. Compound if statements should look the same
as simple if statements.

Mr Levy's style of if statement has an equivalent in English called 
"the run-on-sentence". What's silly about the whole thing is that a program
formatter can make this stuff QUITE readable, and will probably find the
bug that "bit" Mr Smith.

The most important element of a readable programming style is the use of white
space. I personally can't stand the K&R style because I get visually confused
when I read it. It's similar to an English paragraphing that doesn't use
indenting or spaces between paragraphs. In the C book itself, this isn't bad,
because the program fragments are small and the structures are simple.
In real programs, however, there are lots of programs which are unreadable
until passed through "indent" (on 4.2).


Chris Shaw    watmath!watmum!cdshaw  or  cdshaw at watmath
University of Waterloo
In doubt?  Eat hot high-speed death -- the experts' choice in gastric vileness !



More information about the Comp.lang.c mailing list