wagar (10/27/82)
I have not seen my favorite C bracing style appear here yet. I believe it is the most clear and consistent, and I will justify that and provide a filter for this style below. In my style, all blocks are simply placed on the same line, without unnecessary spacing. A conditional is thus: if(conditional){statement;statement;statement;} The advantages to this style, and some fine points, are: 1. Left braces appear to the LEFT of right braces. This simple concept is not obeyed by any of the other bracing styles. 2. Comments are not permitted. This is the only wholly consistent commenting policy I have seen so far. While the following conditional may seem clear to some people, others may be confused: i/*f and only i*/f (a =/*equivalence*/= b) /*of*/ co/*urse, here we co*/ntinue; Comments are thus considered harmful. 3. Code will compile faster, and take less disk space, since unnecessary white space and comments are omitted. 4. This style is ideal for the following self-printing C program: main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} I dare anyone out there in net.land to do this with one of the preferred styles! Below is my filter to converts any C program to this style. Note that the Berkeley cb program has 358 lines, whereas mine has only 4. #include <stdio.h> #include <ctype.h> #define ii(c)(isalpha(c)||isdigit(c)||c=='$'||c=='_') main(){char w[512];register char*wp;register int c,lc;int as,ns,pl,pn;lc='\n';pn=1;while(1){tp:while(isspace(c=getchar())){lc=c;if(pl&&c=='\n'){putchar('\n');pn=1;as=pl=0;}}if(c=='#'&&lc=='\n'){if(!pn)putchar('\n');pl=1;while(isspace(c=getchar()));ungetc(c,stdin);c='#';}if(c==EOF)exit(0);wp=w;do{if((as&&(ii(c)||c=='\''||c=='"'))||(ns&&(c=='-'||c=='*'||c=='&'||c=='+')))if(pl!=1&&pl!=2)putchar(' ');as=ns=0;if(c=='"'){pn=0;putchar(c);while((c=mg())!='"'){putchar(c);if(c=='\\')putchar(c=mg());}putchar(c);goto tp;}if(c=='\''){pn=0;putchar('\'');putchar(c=mg());if(c=='\\')putchar(mg());putchar(c=mg());if(c=='\'')goto tp;if(c=='\\')putchar(mg());putchar(mg());goto tp;}if(c=='/'){if((c=mg())=='*'){sk();if(wp==w)goto tp;goto cn;}else putchar(*wp++='/');}putchar(*wp++=c);cn:;}while(!isspace(c=getchar())&&c!=EOF);lc=c;if(pn=(pl&&c=='\n')){putchar('\n');pl= -1;}else if(pl&&pl++==1)putchar(' ');if(wp==w||c==EOF)exit(0);as=ns=0;if(wp[-1]=='='){if(wp-1==w||(wp[-2]!='='&&wp[-2]!='<'&&wp[-2]!='>'&&wp[-2]!='!'))ns=1;}if(ii(wp[-1])&&pl>=0)as=1;if(pl==-1)pl=0;}}sk(){register char c;while(1){c=mg();g:if(c=='*')if((c=mg())=='/')return;else goto g;}}mg(){register char c;if((c=getchar())==EOF)exit();return(c);} -Steve Wagar decvax!yale-comix!wagar
sjb (10/28/82)
If it's time for dares, I dare you to ask any programmer to help you debug a program like that!
CSvax:cak (10/28/82)
Ah, but your beautifier has a goto in it! How gauche!
dmy (10/28/82)
Now that ' s better ! A man after my own heart . Thanks , Steve . ( Did you see my self - printing program on the first line ? Still has one bug , though . ) -- dmy --
pcl (10/29/82)
I thought I might share with you all an interesting anecdote brought to mind by Steve Wagar's suggested bracing convention of if(conditional){statement;statement;statement;} In my first year of grad school, I took the (required) data structures course. In addition to the obvious things such a course might cover, at this school it was also supposed to cover some elements of structured programming. Well, the professor I had preferred a style quite similar to Steve's above: embedded spaces were allowed, but you were to continue on the same line (card, actually) until no more words would fit, and only then move to a new line. The only exception to this was starting a new procedure. The advantages he saw were somewhat different from those described by Steve: 1. You used fewer cards for your program. Besides being good for the environment, this made it less likely that you would drop your deck. 2. You were much less likely to get two cards reversed and not notice, which is so easy to do with that "one statement per line" style. 3. Your program would compile faster, thus charging you less per run while debugging your program. [Students had a fixed dollar allocation per semester for computer use in each course.] 4. You would be charged less, since fewer cards were read on each run. 5. You would be forced to write your entire program up ahead of time, and not try to compose "at the keypunch" (which we all know is a terrible practice). When I was so perverse as to insist on starting each statement on a new card, and *indenting* those statements to reflect the block structure, he (reluctantly) agreed not to take off any points! [By the way, I had learned these nasty habits in the three previous years of working in BASIC - mostly from maintaining code that didn't follow them.] This was not that many years ago, either! I think I'll leave the school and professor un-named, except to say that the professor was a senior faculty member, whose earlier work is cited in Knuth. Also, the teaching assistant was Mark Horton; I think it was even harder for him trying to help the others in my class, than it was just being a member of that class. Paul Lustgarten Bell Labs - Indian Hill