why indentation should be part of the syntax

Chris Angelico rosuav at gmail.com
Sun Mar 2 09:08:24 EST 2014


On Sun, Mar 2, 2014 at 11:36 PM, Ned Batchelder <ned at nedbatchelder.com> wrote:
> As much as I like indentation as syntax, and am amused by this bug, I'm not
> sure we can chalk it all up to space-vs-brace. After all, who would have
> wanted two goto's in a row even with braces?  It's not like there's some
> engineer at Apple who meant for the code to read like this:
>
>     if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) {
>         goto fail;
>         goto fail;
>     }
>
> This looks to me like a poorly handled merge conflict maybe?  I wonder if
> we'll ever get the details.

Put it this way: If I saw two gotos in a row like that, with or
without braces, I would be firing up gitk or git gui blame or
something to figure out what happened.

But ultimately, it comes down to this: No matter what, the compiler
can't catch every bug. That's why we have peer review, test suites,
and (above all) the magnificent time machine of source control. "What
the <<bleep>> happened here?" becomes "Huh. I see, someone didn't
notice that x and y did a z" when you can see the commit that
introduced something. Indentation-as-syntax doesn't prevent weird
errors from creeping in - look at this one from the Python stdlib:

http://bugs.python.org/issue20729

Notably this bit of code:

        if hasattr(arg, 'iteritems'):
            source = arg.items()
        elif hasattr(arg, 'items'):
            source = arg.items()
        else:
            source = arg


It would take an exceedingly weird set of circumstances for this to
actually matter, but it's something that looks pretty odd in the code,
and no compiler support would help it. Although... to plug my own
proposal... PEP 463 exception expressions might have reduced the
duplication a bit. But it'd still be possible to get something that
looks weird, like:

source = (arg.items()
    except AttributeError: (arg.items()
    except AttributeError: arg))

or, equally weirdly:

source = ((arg.items()
    except AttributeError: arg.items())
    except AttributeError: arg)

There's no "easy fix" for this sort of thing. Maybe a linter could
have been run over the original C code and flagged a warning
(Unexpected indent!), but that's really all. (That actually goes
against the subject line. If the indentation is what matters, you
can't lint it against the braces, you just get the other odd
behaviour.)

ChrisA



More information about the Python-list mailing list