Not coalescing around None-aware
Not coalescing around None-aware
Posted Dec 23, 2022 6:22 UTC (Fri) by zorro (subscriber, #45643)In reply to: Not coalescing around None-aware by Nahor
Parent article: Not coalescing around None-aware
Posted Dec 23, 2022 10:42 UTC (Fri)
by mb (subscriber, #50428)
[Link] (4 responses)
I don't agree with that.
There are many patterns in programs and languages that a reader doesn't literally read and parse any more after getting used to it. And that pattern matching doesn't really depend a lot on the verboseness of the code.
Take for example a basic iteration loop in C:
for (int i = 0; i < length; i++)
Nobody (with experience) reads and parses that step by step.
The same thing happens with
data if data is not None else default
The big advantage of the verbose form is that you can *also* read and understand it, if you don't know the pattern. You can't do that with special syntax like ??.
Posted Dec 24, 2022 13:30 UTC (Sat)
by kid_meier (subscriber, #93987)
[Link] (1 responses)
I also don't think it's fair to say that special syntax always increases mental load. Your pattern matching machinery will work on the special syntax just as well as it works on the fully spelled out syntax--actually quite possibly better since there is less risk of false positives. As someone who writes Java for a living, I think it'd be a good trade. Java already has ternary operator but I'd still welcome a more specialized operator like this.
Posted Jan 5, 2023 15:33 UTC (Thu)
by kpfleming (subscriber, #23250)
[Link]
The example in the post you referred to may be the 'normal form', but remove the 'not' from it and the meaning changes significantly but a 'quick read' to look for patterns might overlook the change. I can't imagine an actual use for that expression, but I'd hate to misunderstand it because my brain assumed it was just the same as all the others it had seen.
Posted Dec 25, 2022 6:21 UTC (Sun)
by Nahor (subscriber, #51583)
[Link] (1 responses)
You're mixing cause and effect.
Note though that it will never be as fast as the "??" operator. In big part because you need to check that the words are what you expect them to be (Is the "data" after the "if" really "data" or is it "date" or "dota"? Is the "None" really "None" or is it "Note" or "Noon"? ...). Because of the more limited scope of "??", some of those questions just become irrelevant / have a single and necessary answer.
And that familiarity goes hand in hand with Zorro's comment. The "??" operator, while initially not expressive, will become more expressive as you get familiar with that construct.
> The big advantage of the verbose form is that you can *also* read and understand it, if you don't know the pattern
And we are back to "compact" vs "expressive". One is expressive but verbose, the other is compact but less expressive. One is more readable for newbies, the other for veterans. And people don't agree where the balance point should be.
> Saying that people don't have to [learn] ?? is also wrong, because other people will use it in their code and examples that Newbies will read
Nobody said anything like that. Everybody has to learn. Even the long expression needs to be learned for that matter, because it's not common to have the true-statement on the left of the "if", or that statement that returns a value ("else default") (*). The amount of learning effort is not the same, but it's still there in both case.
(*) more specifically, in most languages, "if" is a statement, which cannot have a value, while here in python, the "if" is an expression, which do have one.
Posted Dec 28, 2022 15:14 UTC (Wed)
by marcH (subscriber, #57642)
[Link]
Except for people who have to make a small change to a Python script rarely, only a couple times a months. I have and always had teammates in that case and that's where Python really shines compares to shell scripts or Perl or others. While the verbose " if None" variations annoy me every time, I totally understand the "Perlification" fear of "??".
Isn't there some middle ground alternative?
Not coalescing around None-aware
It's immediately clear to the reader that this is probably iterating over an array of some kind and i is the index.
Special syntax always increases the mental load and increases the initial learning effort. (Saying that people don't have to lean ?? is also wrong, because other people will use it in their code and examples that Newbies will read).
Not coalescing around None-aware
Not coalescing around None-aware
Not coalescing around None-aware
"verbosity" is not about "how easy it is to read something", it's about the number of tokens/words needed to say something.
Then based on your familiarity with a given expression, the verbosity will have a varying impact on your parsing/understanding speed.
Familiarity will make things more readable in all cases, either by making things appear more compact/less verbose by skipping some parts, or by making them more expressive as it gets more ingrained.
Not coalescing around None-aware
> Familiarity will make things more readable in all cases,...