[Python-ideas] Inline assignments using "given" clauses
steve at pearwood.info
Fri May 11 12:17:24 EDT 2018
On Fri, May 11, 2018 at 12:37:43PM +0100, Rhodri James wrote:
> while (cmd := get_command()).token != CMD_QUIT:
> while cmd.token != CMD_QUIT given cmd = get_command():
Okay, considered. I think the first is preferable.
Much earlier in the PEP 572 discussion, I strongly argued in favour
expr as name
syntax on the basis that the most important part of the overall
expression is "expr", not the assignment target, and therefore that
should come first. Even though I have accepted that "as" is not viable,
I still believe that it is preferable to have the expression first, or
if not first, at least as close to the left as we can get it.
This "given" syntax puts the expr part all the way to the far right of
the line. A line which is made all the longer for needing to use "given"
and redundantly state the target name.
It's like we're trying to maximize the distance the eye has to travel
back and forth when reading.
I have to read to the end of the line before I have any idea where cmd
has come from or what it is. The fact that it comes from a "given"
expression comes as a surprise at the end of the line.
Now obviously this doesn't matter if I'm reading lines of code in
careful detail, but I don't do that all the time. I skim code far more
than I read it in careful detail, and the closer things are to the left,
the more likely I am to see them while skimming. The further out they
are, the easier they are to miss.
I think that "given" will *literally* make reading harder, in that the
eye has to travel further to spot the relevant expression while skimming
over code. As I said, I don't think it makes any difference when reading
closely in detail. But most of my reading of code is skimming to find
the relevant line or section, and then read closely. I would probably
skim a hundred lines for every one I read closely.
We read more code than we write, but writing is important too. I think
the verbosity of "given" (six chars versus two) and the redundancy of
needing to repeat the name of the target even if you only use it once
will soon make using this syntax seem like a chore.
More information about the Python-ideas