[Python-ideas] Optional static typing -- the crossroads
Nicholas Cole
nicholas.cole at gmail.com
Sun Aug 17 11:52:52 CEST 2014
On Sun, Aug 17, 2014 at 9:50 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 17 August 2014 18:34, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> Declaring types in the function parameter list is very common, in many
>> languages. If it were *that* much harder to read, languages wouldn't
>> keep using it. (Not many languages follow Forth or APL syntax.) Perhaps
>> because I learned to program in Pascal, I find the annotation syntax
>> very easy to read, but, yes, anything which increases the density of
>> information per line risks hurting readability a little.
>
> I once had the "pleasure" of inheriting some code written in K&R style
> C, where the parameter type declarations were separate from the
> signature line:
>
> void foo(a, b, c)
> double a;
> char b;
> {
> ...
> }
>
> ANSI C, with inline typing, is far more readable :)
I think you've put your finger on it. It comes down to a disagreement
over density of information. You'd like everything in "one pass" as
it were. The way my brain is wired, I read your example here as:
"This function takes three (non-named) parameters, the a is a double,
the b is a char." I find that faster to process than the inline
alternative, especially when the alternative is optional.
With meaningful parameter names it would be even easier.
If I re-write your example:
> void foo(double a, void b, c)
My brain takes an extra fraction of a second to count the number of
arguments. Syntax highlighting would help, of course.
Some of this can be very subtle. For example, it's important for
readability that in Python positional parameters are all specified,
then keyword ones, so the brain doesn't have to keep switching
backwards and forwards.
In C, of course, everything has to be typed, and so I can see it makes
sense to put it all inline. But what if you are mixing the two, and
some are typed and some not? I think it is all going to get very
dense and hard to read.
The same tension occurs in natural languages. I tend to write quite
dense English prose, myself. My editors always want me to write less
densely, and over time I've come to see that they are right! Dense
prose is fine for the specialist, but it doesn't help the student or
the casual reader.
N.
More information about the Python-ideas
mailing list