What is structured programming (was for/while else doesn't make sense)

Rustom Mody rustompmody at gmail.com
Thu Jun 16 01:48:33 EDT 2016


On Thursday, June 16, 2016 at 8:25:10 AM UTC+5:30, Lawrence D’Oliveiro wrote:
> On Thursday, June 16, 2016 at 1:51:54 AM UTC+12, Random832 wrote:
> > ... and in particular it does not establish that break is in any way
> > less structured than any other constructs that have keywords.
> 
> Interesting that those who objected to my use of while-True-break loops could 
> not come up with any unified alternative way of writing them. Every single 
> example I posted had to be handled differently, as opposed to my common way of 
> dealing with them.



Thanks for raising the example of your code -- I hesitated to do so :-)
It answers better than I could Steven's very interesting question:
"What's your definition of structured?"

Everyone -- including Steven -- who commented on it did so with a variation
on "Ugh!" , "Yikes!" etc.

If you (Steven) takes these subjective/emotional outbursts and tries to 
objectivize them -- "What's there to ugh about?" I believe you will come to
some definition of structured.

Of course we could give a more formal definition -- I am not sure that line
is so interesting.

Should admit that I tried to look up 'structured' and found precious little¹

So here is the formal definition I remember from decades ago:

A structured flow-graph is one that has a single point of entry and exit.
And is recursively made from smaller structured flow graphs
With a finite set of 'graph combinators'

A structured program is one who's flow graph is structured

As I said I dont find this definition very useful since
break is unstructured as is return, yield and much else.

Cognitively though it's the other way round: goto/break-filled code is -- in 
some informal sense -- more unstructured than return-filled code.
And Ian's yield based code cleans up -- at least to most of our eyes/noses! -- 
your break-based code.

Note that in terms of the formal definition, yield jumps around more than 
return jumps around more than break.

Yet informally we (at least I) think of the generator
def gen():
  yield 1
  yield 2
  yield 3

not in terms of control flow but as data: a lazy, space-non-wasting form of
the list [1,2,3]

So what's a USEFUL definition of structured?? Interesting question!

-----------------
¹ Which probably proves that I am fast getting into the generation for which the
picturesque Tamil saying applies: "House says Go! Go! Grave says Come Come!"



More information about the Python-list mailing list