[comp.unix.wizards] Indenting and alignment style

avr@mtgzz.att.com (a.v.reed) (12/07/88)

The real issue is readability. I think even Henry Spencer would agree
that the goal is to make code as easy to read and understand as
possible. This in turn is influenced by matching style to the human
visual system and mind - objective readability - and by how close the
style is to what one is used to. Objective readability (on which there
is a vast literature in psychology, human factors and education
journals) suggests that the optimal style would provide the reader with
vertical alignment of the closing brace with the opening brace, and of
immediately enclosed text with the enclosed braces, like this:

	function(argument,argument)
		{
		statement;
		statement;
		}

If everyone were starting from scratch, this would be readily accepted
by everyone. But since that is not the case, we have to deal with the
fact that objective readability does not necessarily optimize relative
readability, especially for individuals who are used to (and have what
psychologists call "overlearned skills" for processing) an objectively
suboptimal but, in the C community, traditional style:

	function(argument,argument){
		statement;
		statement;
	}

With this style, the reader cannot use vertical alignment for conceptual
matching, either between the corresponding opening and closing brace, or
between the braces and the text they enclose. But people who have been
reading code in this style for many years have developed compensating
skills, and insist that vertically aligned code is "difficult to read" or
even "ugly". And for/to them, it certainly is.

The solution, I think, is to write code in the style that's best for
you, and use "prettyprinting" tools when reading or editing code written
in a different style by somebody else. That way, new programmers can
learn the (objectively optimal) vertically aligned style without unduly
inconveniencing the "old dogs" and traditionalists. After all, *tools*
are why we use UNIX(R) and C in the first place. Insisting that everyone
conform to what you happen to like and/or are used to isn't just
religion, it is neanderthal religion. It'll make you miserable. So just
filter the code you read through the prettyprinter of your choice, and
be happy. OK?
				Adam Reed (avr@mtgzz.ATT.COM)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (12/08/88)

In article <4733@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
: 
: 	function(argument,argument){
: 		statement;
: 		statement;
: 	}
: 
: With this style, the reader cannot use vertical alignment for conceptual
: matching, either between the corresponding opening and closing brace, or
: between the braces and the text they enclose.

You can call it "overlearned skills" or whatever, but when I see a
closing brace I don't WANT to find the opening brace--I want to find
out what the predicate controlling the block is.  I just want to look
straight up and see the function, or the "if", or the "for", or whatever.

There may be a large body of psychohysterics that proves that vertical
alignment of braces with enclosed statements enhances syntactic
understanding, but productivity arises from *semantic* understanding.
Let the compiler check the syntax.

There will always be a fight between the "combers" and the "danglers",
I fear.  Each side can muster "objective" proof.  (There can be little
or no objective proof of what works better for experts, actually,
because experts are ineligible for experiments that require "naive"
subjects.  But naive subjects aren't going to have the same goals as
experts.)

: The solution, I think, is to write code in the style that's best for
: you, and use "prettyprinting" tools when reading or editing code written
: in a different style by somebody else. That way, new programmers can
: learn the (objectively optimal) vertically aligned style without unduly
: inconveniencing the "old dogs" and traditionalists. After all, *tools*
: are why we use UNIX(R) and C in the first place. Insisting that everyone
: conform to what you happen to like and/or are used to isn't just
: religion, it is neanderthal religion. It'll make you miserable. So just
: filter the code you read through the prettyprinter of your choice, and
: be happy. OK?

A nice happy world-peaceish thought, but it's not that simple.  If I
prettyprint somebody else's program, I can no longer generate patches
that are easy to apply to the original code.  Nor can I easily apply
patches that the originator sends out.  For better or for worse, the
"real" code needs to remain in the original format.  (You could
conceivable prettyprint a copy just for perusing, but few people are
going to take the trouble.)

Maybe neanderthal religion is the best we can do.  It's overstating
things, however, to say that this must make me miserable.  A bit grim,
but not miserable.

Combers
    of the world
Unite!						:-)

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

ok@quintus.uucp (Richard A. O'Keefe) (12/08/88)

In article <4733@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
> Objective readability (on which there
>is a vast literature in psychology, human factors and education
>journals) suggests that the optimal style would provide the reader with
>vertical alignment of the closing brace with the opening brace, and of
>immediately enclosed text with the enclosed braces, like this:
>
>	function(argument,argument)
>		{
>		statement;
>		statement;
>		}
>
Please cite a study which shows this.
In a language which uses keywords 'start' 'finish' instead of braces,
this is demonstrably silly:
	start
	if x > y ...
	while ...
	finish
lines the brackets up with the keywords of statements that have nothing
to do with them.  Surely it is clear that to avoid this kind of visual
confusion, the brackets must be elsewhere
	<opening bracket>
	<indent><statement>...
	<indent><statement>...
	<closing bracket>

The "comb" style as recommended for ADA works well in nearly every
language.

avr@mtgzz.att.com (a.v.reed) (12/10/88)

In article <823@quintus.UUCP>, ok@quintus.uucp (Richard A. O'Keefe) writes:
> In article <4733@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
> > Objective readability (on which there
> >is a vast literature in psychology, human factors and education
> >journals) suggests that the optimal style would provide the reader with
> >vertical alignment of the closing brace with the opening brace, and of
> >immediately enclosed text with the enclosed braces, like this:
> >
> >	function(argument,argument)
> >		{
> >		statement;
> >		statement;
> >		}
> >
> Please cite a study which shows this.

There were 3 empirical assertions in my argument on this:

1. Comprehension of computer programs is enhanced by cues for matching
each closing delimiter with the corresponding opening delimiter.

2. Comprehension of computer programs is enhanced by cues for matching
enclosed statements with the enclosing scope delimiters.

3. Vertical alignment is an effective matching cue for one-character
delimiters.

With respect to (1), a good study which shows this is Sykes, Tillman,
and Shneiderman, The effect of scope delimiters on program
comprehension, Software Practice and Experience 13, pp 817-824 (1983).

With respect to (2) and (3), a good empirical study is Krall, A., and
Harris, W., An investigation of the effects of program style on the
readability/understandability of a simple COBOL program: the effects of
indentation and vertical spacing. University of Maryland Research
Report, 1980. (A good summary of the findings of Krall and Harris 1980
is available in Miara, Musselman, Navarro, and Shneiderman, Program
indentation and comprehensibility, Communications of the ACM 26, pp
861-867, 1983, on p. 862).

And the above are not necessarily the best or most definitive studies
for the above; just the ones that I happened have on hand right now.

					Adam Reed (avr@mtgzz.ATT.COM)

henry@utzoo.uucp (Henry Spencer) (12/10/88)

In article <4733@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
>The solution, I think, is to write code in the style that's best for
>you, and use "prettyprinting" tools when reading or editing code written
>in a different style by somebody else. 

This would be the solution if prettyprinters were smart enough to equal
the job done by a conscientious programmer.  They aren't.

>... Insisting that everyone
>conform to what you happen to like and/or are used to isn't just
>religion, it is neanderthal religion...

No, it's called "having standards", actually.
-- 
SunOSish, adj:  requiring      |     Henry Spencer at U of Toronto Zoology
32-bit bug numbers.            | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

tbray@watsol.waterloo.edu (Tim Bray) (12/12/88)

In article <1988Dec9.190711.4119@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>>... Insisting that everyone
>>conform to what you happen to like and/or are used to isn't just
>>religion, it is neanderthal religion...
>
>No, it's called "having standards", actually.
Hear, hear.  It is totally impossible to get more than about 3 competent
Unix hacks unanimous about the ideal coding style.  The important thing
though, is that

1. most of the popular coding styles are actually pretty good.  
2. once your hindbrain has got used to a particular coding style, reading
   code in that style gets easier and easier, and it becomes clear that
   struggling with nonstandard code is a COMPLETE WASTE of intellectual 
   effort (maintaining software is difficult enough already).

So I think for any project, somebody in charge should get fascist and
enforce some (it doesn't really matter which) coding style with an iron
hand.  In fact, even arguing about coding style is an (entertaining)
waste of time.
Cheers, Tim Bray, New OED Project, U of Waterloo

ok@quintus.uucp (Richard A. O'Keefe) (12/12/88)

In article <4745@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
>In article <823@quintus.UUCP>, ok@quintus.uucp (Richard A. O'Keefe) writes:
>> In article <4733@mtgzz.att.com> avr@mtgzz.att.com (a.v.reed) writes:
>> > Objective readability suggests that the optimal style would provide
>> > the reader with vertical alignment of the closing brace with the opening
>> > brace, and of immediately enclosed text with the enclosed braces,
		=====================================================					
>> > like this:
>> >	function(argument,argument)
>> >		{
>> >		statement;
>> >		statement;
>> >		}
>> Please cite a study which shows this.

>There were 3 empirical assertions in my argument on this:

>1. Comprehension of computer programs is enhanced by cues for matching
>each closing delimiter with the corresponding opening delimiter.

Agreed.

>2. Comprehension of computer programs is enhanced by cues for matching
>enclosed statements with the enclosing scope delimiters.

"matching" -- no way!  "associating" -- of course.

>3. Vertical alignment is an effective matching cue for one-character
>delimiters.

Agreed.

However, from *NONE* of these three points can the claim I have underlined
be deduced.  The statement (which I accept, of course!) that vertical
alignment is an effective way of associating things tells us that the
statements, not being at the same syntactic or conceptual level as the
brackets, would benefit from being indented.  Hence the old Algol 60
convention:
	if <condition> then
	   begin
	      <decls>;
	      <stmts>
	   end
	else
	   begin
	      <decls>;
	      <stmts>
	   end

Let me make my request clearer.  Can anyone cite a study which shows
	if (...)
	    {
	    <stmt>
	    <stmt>
	    }
to be superior to
	if (...)
	  {
	    <stmt>
	    <stmt>
	  }