Why not allow empty code blocks?

Chris Angelico rosuav at gmail.com
Sun Jul 24 08:48:20 EDT 2016


On Sun, Jul 24, 2016 at 10:17 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> (Do other professions make arguments like this? Do carpenters, say, argue
> against nail guns because "well if you accidentally hold a loaded nail gun
> to your head, then press the trigger, bad things will happen"? Or is it
> just programmers who make a common practice of arguing that the programming
> language should protect the user from sheer carelessness?)

What about "if you accidentally drop a loaded nailgun, it will fire
sharp bits of metal at high velocity in all directions"? I'd say the
only difference is that carpentry has been around longer, so they had
all these arguments a long time ago and settled on safe ways of doing
things. Why has (most of) the programming world advanced from naive
text editors to programming editors with syntax highlighting, linters,
and other features? Because we are more efficient when our tools
protect us from mistakes. Did people argue a few decades ago about the
value of syntax highlighting? Personally, I took a long while to get
from "well, it'd be nice, but I won't go to any serious effort for it"
to "it's my normal way of coding, and anything that DOESN'T have it is
for quick tweaks only". Also, why does CPython keep an eye on things
like Coverity? If the core devs are competent, they should be able to
write code that never dereferences null pointers, mismatch
INCREF/DECREF, or any of those other errors - right? But life's better
with protection.

It's a trade-off. How much effort do you want to go to? Duplicate work
in daily operation is a high price to pay, which is why Python doesn't
demand "indentation AND 'end' keywords", but protection does have
value. Maybe the people who are most worried about this can enact a
simple rule: no dedent without a blank line? That can easily be
verified by a script, and it'd protect against most of the given
examples. It's not too much effort (after any reasonable-sized block
you'll probably have a blank anyway, so it's only the tiniest of loops
that would be affected). And no language changes are needed :)

ChrisA



More information about the Python-list mailing list