What's the best way to minimize the need of run time checks?

Steven D'Aprano steve+python at pearwood.info
Fri Aug 12 07:07:12 EDT 2016


On Fri, 12 Aug 2016 07:38 pm, BartC wrote:

> 'year' has been spelled wrongly

How do you know?

The point is that it is intentional that callers can set arbitrary
attributes on (most) objects. It is perfectly legitimate to set:

d.year
d.century
d.owner
d.extra_seconds

and, yes, even d.yaer, since it isn't the compiler's job to check your
spelling or critique your choice of variable names.

If you want to spell check your code, run a spellchecker[1], or a linter, or
have a human review it. Or all three.

There are broadly two tactics that a language can take when it comes to
attribute (or more widely, variable) names:


(1) Editing is cheap, but compiling and testing code is expensive, and the
consequences of accessing a non-existent variable are catastrophic
(overwriting arbitrary memory, accessing uninitialised memory, segmentation
faults, execution of arbitrary code, security vulnerabilities, etc).
Therefore it is important that the compiler check ahead of time that every
name referenced exists and has been declared.

(2) Compiling and testing code is cheap, and the consequences of accessing
non-existent variables are mild. Since typos are generally rare (say, one
name out of a hundred is mispelled), requiring declarations is a waste of
effort. Rare errors will be caught through human review, linters, and
testing.


The first time I ever compiled a full-sized application (not a particular
large one either, it was a text editor a little more featureful than
Notepad) it took something like nine hours to compile on a Mac SE (this was
circa 1990). How mad would I have been if, eight hours and thirty minutes
into the compilation, the compiler suddenly stopped with an error caused by
a mistyped variable name?

Today, I could probably compile the equivalent program in a minute. Do I
care if the compiler doesn't pick up the typo until 58 seconds through the
process? Not a whit. I would care if the compiler doesn't pick up the error
*at all* and I'm writing in a low level language where accessing a
non-existent variable allows Bulgarian hackers to steal the money in my
bank account, so there's still some advantage to having declarations in
low-level languages.

It *absolutely makes sense* to require variable declarations (possibly
including type information, where it cannot be inferred) when the
consequences of a typo are routinely expected to be severe. Under those
circumstances, a "bondage and discipline" language that forces you to be
absolutely pedantic to satisfy the compiler can be a good thing.

Turn it around though: one of the reasons why languages like Ruby,
Javascript, Lua, Python, Perl etc are so ridiculously productive is that
you don't have to keep stopping to declare variables just to satisfy the
compiler. Need a new variable? Just assign to it and keep going -- there's
no, or very little, mental task switching.

Another reason why such languages are so productive is the availability of
an interactive REPL where you can try out code interactively. How clumsy
and awkward the REPL would be if you had to keep declaring variables ahead
of time.

The cost of that flexibility is that I have to pay a little bit more
attention when doing a code review, I have to write a few more tests. But
the edit-compile-run cycle is so ridiculously fast that this doesn't really
matter. Programmers have invented entirely new coding paradigms, Test
Driven Development and Agile, to take advantage of this speed and
flexibility. Sure, you can take TDD and Agile into the realm of statically
typed languages, but it will always be like running a sprint wearing a suit
of armour. You can do it, but you'll be out-paced by the sprinter wearing
track shoes and Lycra.





[1] Are there programming language aware spell checkers? If not, there
should be.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list