C bites / programming style

Adam V. Reed adam at npois.UUCP
Sat Sep 7 07:58:48 AEST 1985


Michael Shannon {ihnp4,hplabs}!oliveb!3comvax!mikes writes:

> With regard to changing
> 
> 	if(condition) {		 |		if(condition)
> 		s1;		into			{
> 		s2;		 |			s1;
> 	}			 |			s2;
> 				 |			}
> 
> [the idea is to indent the braces so that they are in the same column as
> 	the statements they enclose]
> 
> 	Why do you like this style?  This seems to indicate that
> the braces are associated in your mind with the enclosed statements.
> 	In my mind, the braces are associaed with the 'if' statment, and
> so they should be indented at the same column as the 'if'.  I look for
> the closing brace in the same column to be a sort of 'statement terminator'
> (i.e. when I see it, I think "Ah, the end of the if statement").
> 	I've seen this unusual indentation style before, and I'm curious
> if you developed a preference for it in learning some other language?

According to K&R, Appendix A, section 9, the braces together with the
declarations and statements they enclose form a single syntactic entity
called "compound statement" or, equivalently, "block". This means that
the braces are conceptually associated with the statements they enclose,
and NOT with anything before or after. I use the form on the right
exclusively, because I find it easier to keep a "block" together in my
mind if it looks like, well, a single BLOCK. Except for people who have
assimilated some very sloppy mental habits, programs are easier to read
if visual entities correspond to syntactic and conceptual entities. I
learned to align corresponding opening and closing symbols while coding
in LISP, but I found the underlying psychological principle to be
equally applicable to every language I encountered in the last 20 years.

				Adam Reed (ihnp4!npois!adam)



More information about the Comp.lang.c mailing list