Friday Finking: Contorted loops

Peter J. Holzer hjp-python at hjp.at
Sun Sep 12 05:43:58 EDT 2021


On 2021-09-11 21:38:02 -0400, Avi Gross via Python-list wrote:
> Peter, in your own personal finite sample, I am wondering what you might do
> TODAY if you looked at your loops again and considered redoing them for an
> assortment of reasons ranging from using the code for teaching to efficiency
> to just fitting your mood better?
> 
> I have seen seasoned authors go back to their early work and groan.

Yeah, I do that. (Un)fortunately I also have other people's code to
groan about so I won't despair too much about the stupidity of my
younger self.


> My guess is that many of us (meaning myself included) often approach a
> problem and go with the first thing that comes to mind. If it fits well
> enough, we move on to the next thing we can do. If not, we may step back and
> evaluate multiple additional options and try another tack. 
> 
> I have seen not of sort-of redundant code because someone did not plan ahead
> and realize something very similar might be needed later and thus did not
> make a general function they could re-use. Occasionally they may later go
> back and re-do but often, not so much and just keep copying lines and making
> minor modifications. Same general idea.

That certainly happens. I am a bit overly conservative and try to get
away with minimal code changes even if a complete reimplementation of
that unit would be clearly better. Especially if it's someone else's
code and there are no unit tests. But also for my own code.

(As an aside, I notice the same tendency when changing text: Altering an
existing paragraph is hard, especially if someone else wrote it. Also,
while I think I can express myself quite clearly in both German and
English, I'm rarely satisfied when I try to translate between those
languages. I always stick too close to the original).

> And perhaps worse, you may write a loop and later have to keep adding code
> to deal with new requirements and special cases and rather than pause and
> analyze and perhaps start again with a cleaner or more easily extendable
> solution, just keep grafting on things to make the darn current code work.
> Code that has many ways to exit a loop is often an example of this
> happening.

That too. Those little C utilities I mentioned are probably a bad
example because they are so small and had little reason to evolve. But I
do have Perl scripts which I originally wrote 20 years ago and which are
still in use and have been adapted to changing business requirements
again and again in that time. Those do contain some gnarly code.


> So if you looked at your own code now, in the context of the rest of your
> code, would you change things?

Almost certainly. Especially in C I would probably be more cautious
about undefined behaviour now and for different reasons. Back in the
90's I mostly worried about portability: That code could one day run on a
36-bit ones-complement machine with 9-bit chars. These I days I worry
more about overly aggressive optimizations: That pointer is accessed
here so it can't be null, so it can't be null here either so that check
can be optimized away.

I started using Python only 7 years ago, when I had already been using
Perl for almost 20 and C for over 25 years. So my older Python code
probably looks a bit "perly". So they use dicts and map and filter but
not list comprehensions for example. Also some of that code was
partially inherited from other Python programmers who adhered to the
"a real programmer can write Fortran in any language" mindset.


> So when counting the various kinds, are you looking for direct or indirect
> methods too like map/reduce or vectorized operations?

No, because that wasn't the question I was trying to answer. The
question was "do people use do/while loops frequently in languages which
provide them"? I chose C (mostly because it is easier to get useful
numbers with tools like grep and wc than with Perl) and therefore only
the types of loops available in C. (Methodically the main flaw in my
approach is that I only looked at a single language and a single person
and only a tinly sample from that person. To really answer that question
you would have to look at a sizable sample from Github or something like
that).

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20210912/57589621/attachment.sig>


More information about the Python-list mailing list