Friday Finking: Contorted loops

Avi Gross avigross at verizon.net
Sun Sep 12 17:11:58 EDT 2021


Some of what I read makes me chuckle.

Yes, large units of code, and even smaller ones, may be a chore to figure
out. Arguably harder when you use indentation and the next/last parts are
not even on the same screen as the rest. Sometimes you want to use a
split-screen in some editor to line up the two parts or some other
technique.

But my suggestion is to COMMENT things well and I mean too much!

do {
   <CODE>
  } while <COND>

Why not add a comment at the top like:

# The following loop has a WHILE clause controlling it at the end.

do { # until the while clause below
   <CODE>
  } while <COND> # End of the do loop.

My code tends to have brief comments especially when I have nested
constructs such as multiple nested loops or in sequence, or if statements
inside others. The comment often looks like

... # END of inner if 

... # END of outer if

The point is that places where the way of writing a program may not be as
obvious as you want, may be the places you comment to make up for that.

Do people read the comments? Are they extra verbiage or in the way? Who
knows. And, of course, as I noted earlier, it is one more thing that gets in
the way.

There are languages which allow you to add some kind of labels in the code
and you can label a loop with something like "doo_wop:" and inside a nested
loop, you can break or continue to the named label and thus jump out
multiple levels if needed. The point is not to have that feature, but
perhaps have an option like:

do label {
code
} label while ...

Something that uniquely allows you to associate the end of the loop right
next to the end. Since the label can be anything allowed, it could by
something that suggest it is a while loop.

I appreciate programming environments that let you do complex, often nested,
things. But with great power can come great responsibility to use it well
and make sure others can figure it out.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Peter J. Holzer
Sent: Sunday, September 12, 2021 4:49 PM
To: python-list at python.org
Subject: Re: Friday Finking: Contorted loops

On 2021-09-12 10:28:22 -0700, 2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> On 2021-09-11 at 18:21:17 +0100,
> Alan Gauld via Python-list <python-list at python.org> wrote:
> > On 11/09/2021 15:41, Peter J. Holzer wrote:
> > > How is C's do/while loop more horrible than Pascal's repeat/until? 
[...]
> > so code that has
> > 
> > do{
> > code
> > }
> > while condition;
> > 
> > Looks, for non-trivial cases, like a lot of code followed by an 
> > empty while loop.
> > 
> > The do is easy to miss  and the while loop disguised as a repeat 
> > termination is confusing.
[...]
> (Side question:  why put the "{" next to the "do," but the "}" and the 
> "while" on separate lines?)
> 
> And I would put the while on the same line as the closing brace (which 
> is also where I put the "else" in an if statement):
> 
>     do {
>         code;
>     } while(condition);

Me too.

I also checked two C books from "þe olde times" (K&R, 1st editiion, German
translation; and "A Book on C" by Kelley/Pohl) and both nestle the while on
the same line as the closing brace.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"



More information about the Python-list mailing list