for -- else: what was the motivation?

Avi Gross avi.e.gross at gmail.com
Sun Oct 9 12:18:09 EDT 2022


Smallest code blocks first may be a more modern invention.

Some would argue for a rule related to efficiency of execution. When you
have multiple blocks as in an if-else or case statement with multiple
choices, that you order the most common cases first. Those shorten
execution more often than the rarer cases especially the ones that should
never happen.

There are obvious exceptions like the default having to be last, albeit
some languages allow the default to be inserted anywhere visually even if
the code sort of runs last.

But negating a condition so smaller code appears first may have some cost.
I mean if !function() may be slower as the negating is an extra step. But
it may be even slower if the inversion is done using a wrapper function
that simply inverts the return value from the other function.

I think sometimes a comment placed carefully that explains the code and
logic in concise form is a simpler approach that can be followed by a big
chunk then little chunk without loss of readability.

In the original example the else part can be mentioned before the loop as a
sort of reminder.

In my experience, the size of code often varies within a project so a
smaller chunk may grow as requirements change, such as adding debug or
logging, and large chunks can shrink as common parts of the code get
extracted into functions.

So not a rule but realistically not always a bad idea to write code in a
way that draws the attention of readers along the main path of execution
and perhaps not showing all the checking for odd cases first. I mean as an
example if the argument is of type text then do stuff, else if a number
else if a symbol else if empty  ...

On Sun, Oct 9, 2022, 1:18 AM Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, 9 Oct 2022 at 16:05, Axy via Python-list <python-list at python.org>
> wrote:
> >
> >
> > On 09/10/2022 05:47, Chris Angelico wrote:
> > > On Sun, 9 Oct 2022 at 15:39, Axy via Python-list <
> python-list at python.org> wrote:
> > >> Got it, thanks!
> > >>
> > >> Actually the reason I never used "else" was the violation of the rule
> of
> > >> beauty "shortest block first". With if--else you can easily follow
> this
> > >> rule by inverting "if" expression, but with for--else you can't. The
> > >> loop body of the simplest example is already three lines, in real life
> > >> things are much worse.
> > >>
> > > That's not a rule I've ever been taught; how important is it?
> > >
> > > ChrisA
> >
> > It gets important if the lifetime of your project is more than three
> > months and is extremely important if more than 10 years. But, it depends.
>
> Yes, I'm aware that code readability becomes irrelevant for
> short-duration projects. Beside the point. I'm wondering how important
> it really is to have the shortest block first.
>
> > I also might be wrong in terminology, anyway, there are many rules that
> > make programmer's life easier, described in the literature from the old
> > good "How to write unmaintainable code" to "The Art of Readable Code".
> > And I hope there are a lot of recent books on this subject I did not
> > track and read yet.
>
> Also not really a justification for "shortest block first". Wanting
> some elaboration on that. What's the value in it?
>
> Given that for-else is an excellent, if rarely-used, construct, I
> would say that, *at least*, it is worth setting aside this rule for
> that particular situation. It is also generally worth using fewer
> commas than I just did. Take my advice with a grain of salt.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list