Rules to code by

Physically Phffft jlh at loral.UUCP
Fri Dec 23 07:05:06 AEST 1988


With the fascinating discussion of coding style going on right now, I thought
I'd throw my two cents in.  First, some rules.

1.  Avoid unnecessary white space.  Blank lines waste precious screen real
    estate and paper, while spaces in a line waste disk space.  For this
    reason lines should be indented 1 space and 1 space only.
2.  Never never never use <> for include files.  Any idiot knows that angle
    brackets are the same as double quotes (""), why confuse things?
3.  If you absolutly positively have to use #define for something for
    god knows what reason, at least keep the names short.  Contrary to
    popular belief there's nothing wrong with the letters a, b, c, etc.
    While we're on the subject, parenthesis in macros are to be avoided.
    You don't begin to use the power of cpp until you can fully exploit
    side effects caused by invoking a single macro in different ways.
4.  Don't worry about maintaining your code.  It's common knowledge that
    the way to get ahead in this business is to change jobs every 6 months.
    So go for quantity over style.  This and rule #1 are the reasons why you
    shouldn't comment your code.
5.  Remember, yours is the One True Brace Style (OTBS).  Anybody who doesn't
    use your style is obviously incompetant and you owe it to management to
    point out the incompetance of these novice programmers.  It's a waste of
    time, both yours and theirs, to even discuss the matter with them.
6.  Below I've taken the liberty of re-formatting parts of getopt.c, just
    enough so you can get the flavour of my personal style.  A lot of
    people hate it at first, but then grow to like it.  Note the way
    a function's arguments are on the same line as the function name.
    Also note the way all if statement blocks are kept on a single line
    if at all possible.  For clarity all 'else' statements begin in column
    1 so they stand out.  Before formatting, the code below ran at about
    60-100 lines, clearly you can see the advantage in seeing all of it on
    the screen at a time.  .


=========================

#include "stdio.h"

index(s,c)char *s;int c;{
 while (*s) if (c == *s) return (s); else s++;
 return (NULL);}

int opterr=optind=1,optopt;char *optarg;

getopt(nargc,nargv,ostr) int nargc;char	**nargv,*ostr; {
static char *place = EMSG; char *oli,*index();
 if(!*place){
  if(optind>=nargc||*(place=nargv[optind])!='-'||!*++place)return(EOF);
  if(*place == '-'){ ++optind; return(EOF);}}
  if((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))){
   if(!*place) ++optind; tell(": illegal option -- ");}
   if(*++oli != ARGCH){ optarg = NULL; if (!*place) ++optind;}
else{
   if (*place) optarg = place;
else if(nargc<=++optind){place=EMSG;tell(": option requires an argument -- ");}
else optarg = nargv[optind];
   place = EMSG;
   ++optind;}
 return(optopt);
}

========================


							Jim

PS.  My apologies to Henry Spencer, and all the others who worked on getopt.
PPS. Do I really need a :-) here?  I didn't think so.


-- 
Jim Harkins		jlh at loral.cts.com
Loral Instrumentation, San Diego



More information about the Comp.lang.c mailing list