[comp.lang.misc] SP as "no goto's and nice indentation"

magnus@THEP.LU.SE (Magnus Olsson) (07/30/90)

Much of the recent debate/flame-war about the pros and cons about structured 
programming (SP) has centered around what could be described as

"Dogmatic CS instructors who believe that good programming is the same
 thing as following a rigid set of rules, such as
 'Thou shalt not make use of GOTOs'
 'Global variables are EVIL and must be avoided at all cost'
 'Thou shalt indent your program in the way I like'
 'Thou shalt never put more than one Pascal statement on a line'
 'Thou shalt never write a procedure that doth not fit onto a page'
 and so on."
 
While I think that this caricature is not representative of most CS people
today, I do know that there actually are people who believe that SP is the
same thing as not using goto's and indenting your source code. This
attitude is not confined to one camp - lots of the detractors of SP seem to
share the same misconception (perhaps that's why they don't like SP in the
first place!). 

[Of course, although indentation makes a program more readable, and
excessive use of GOTOs destroys your program structure, SP really is about
organising the flow of control *and* data in a logical way. The current
emphasis in SP teaching (at least here in Lund) seems to be on "information
hiding" and modularity, like in Modula 2 and Ada, or object oriented
programming.] 


Now, this makes me wonder,

Where and when did these rules (about indentation, global variables,
one statement per line, no GOTOs etc) originate? 

Indentation is, of course, as old as Algol, but the emphasis on readablity
seems to be later. I read a classical Swedish Algol textbook from the
mid-sixties (by Ekman and Froberg, it's been published in English, too), and
the example programs in it made rather liberal use of global variables,
GOTOs (and even of arrays of labels), and the program layout was rather
cluttered and not very much like the way you're taught a program should
look nowadays.



By the way, several of the postings mention the difficulty of
implementing a fully recursive language on 'primitive' machines (I know
from the compiler construction course I took that it's quite a lot of work,
even on a VAX). Apparently, most early Algol compilers did *not* implement
recursion. Does anybody have any comments on this? Wasn't this considered
to be a rather glaring omission at the time, or didn't people write
recursive programs back then?


Magnus Olsson		     	| \e+ 	   /_	      
Dept. of Theoretical Physics 	|  \  Z	  / q	      
University of Lund, Sweden	|   >----<	      
Internet: magnus@thep.lu.se	|  /	  \===== g    
Bitnet: THEPMO@SELDC52 		| /e-	   \q	      

gillett@ceomax..dec.com (Christopher Gillett) (07/30/90)

In article <9007300920.AA20411@thep.lu.se> magnus@THEP.LU.SE (Magnus Olsson) writes:
>Now, this makes me wonder,
>
>Where and when did these rules (about indentation, global variables,
>one statement per line, no GOTOs etc) originate? 
>
Djikstra, "GOTOs considered harmful".  I'll dig up the citation 
sometime.  Essentially, he claims that the use of GOTO leads to
bad programming practice, and therefore, should be banned.

I've not encountered anything resembling the first or definitive paper(s)
on indentation, global variables, and the like.  I've seen countless folks
rail against the evils of unstructured code, and then pack their programs
full of global variables.  Of course, I claim that global variables, like
any good programming language construct, are wonderful things when used
properly and in moderation.

/Chris
---
Christopher Gillett               gillett@ceomax.dec.com
Digital Equipment Corporation     {decwrl,decpa}!ceomax.dec.com!gillett
Hudson, Taxachusetts              (508) 568-7172

dgil@pa.reuter.COM (Dave Gillett) (07/31/90)

That's a far more usable definition of SP (if almost as wrong) as the one
that I used to frequently encounter:

  SP - Anything done in a language that provides "ELSE" and "WHILE" and
       doesn't provide (or at least discourages) "GOTO".

                                         Dave

gillett@ceomax..dec.com (Christopher Gillett) (08/01/90)

In article <286@saxony.pa.reuter.COM> dgil@pa.reuter.COM (Dave Gillett) writes:
                                                               ^^^^^^^
                                                               ^^^^^^^
>  SP - Anything done in a language that provides "ELSE" and "WHILE" and
>       doesn't provide (or at least discourages) "GOTO".

I agree with everything this guy says because he's correct, and also because
he has a really nifty last name.  Unfortunately, he doesn't have a really
nifty email address, so I can't tell him in person (bounce bounce bounce :-( ).

:-)
/Chris

---
Christopher Gillett               gillett@ceomax.dec.com
Digital Equipment Corporation     {decwrl,decpa}!ceomax.dec.com!gillett
Hudson, Taxachusetts              (508) 568-7172

sommar@enea.se (Erland Sommarskog) (08/09/90)

(Did this one make it to the outer world? Some other I posted last
week didn't seem to. Apologies if you see this twice.)

Christopher Gillett (gillett@ceomax.dec.com) writes:
>Of course, I claim that global variables, like
>any good programming language construct, are wonderful things when used
>properly and in moderation.

Definitely. But very high moderation. My experience after having
programmed mainly in various modular Pascal dialects and some
Ada is:

* Never, NEVER, jamais, non mai, export a variable from one module
  to another. This will always beat you in the end.

* If you have a one-instance structure, for instance a table, it
  is very good to put it on global level in a module, only
  accessible through calls to that module.

* Sometimes you run in the situation that there are a set of
  parameters you just pass from routine to routine, giving
  all routines seven-eight parameters which is not so nice.
  One solution can be to bundle them in a record type, but
  sometimes putting some of them on global level in the module
  might be the solution. Typically parameters from the environment
  you read once are best handled this way.

Also:
  Declare a variable as close to the point where it used. If you
  can declare variables in blocks like in C or Ada do so. If your
  Pascal compiler allows arbitrary order of CONST, TYPE, VAR,
  FUNCTION/PROCEDURE by all means use it.

It has seldom, if ever, happend to me that I've read other someone
else's code and thought: "He uses too few global variables". But
the opposite is just to common. You see some constants, some type,
and then - wham! 30 global variables waiting for you.
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se