Python "why" questions
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Thu Aug 19 15:13:50 EDT 2010
On Thu, 19 Aug 2010 11:27:18 -0700, Russ P. wrote:
[...]
>> Zero-based counting doesn't entirely eliminate off-by-one errors, but
>> the combination of that plus half-open on the right intervals reduces
>> them as much as possible.
>>
>> The intuitive one-based closed interval notation used in many natural
>> languages is terrible for encouraging off-by-one errors. Quick: how
>> many days are there between Friday 20th September and Friday 27th
>> September inclusive? If you said seven, you fail.
>
> The error mode you refer to is much less common than the typical off-
> by-one error mode. In the far more common error mode, zero-based
> indexing is far more error prone.
So you say, and yet you don't give any examples of this "far more common"
error mode.
>> One-based counting is the product of human intuition. Zero-based
>> counting is the product of human reason.
>
> I suggest you take that up with mathematicians, who have used one- based
> indexing all along. That's why it was used in Fortran and Matlab, among
> other more mathematical and numerically oriented and languages.
Mathematics is an ancient art that values tradition and convention. It
doesn't matter how hard it was to come up with a proof, or how difficult
to verify it. Mathematicians value logical correctness and some
undefinable sense of elegance over how easy it is to verify, the
readability of the notation, resistance to errors, and the ability of
mediocre mathematicians to make useful contributions. Mathematical
notation and conventions are not, by any stretch of the imagination,
designed to reduce errors. Mathematicians consider it a good thing that
they are still mostly using the same notation invented by (say) Leibniz.
Programming language design is a young art that values reliability and
ease of use over tradition. While correctness of the code is important,
equally important (if not more so) is readability, resistance to errors,
and the ability of mediocre programmers to write code that works. Or at
least code that can be hammered into working by slightly less mediocre
programmers. Maintenance of code is usually more difficult than writing
it in the first place, and consequently languages often are designed
specifically to reduce errors rather than to use the exact same notation
than Grace Hopper or Charles Babbage used.
While businesses are conservative in which languages they choose,
language designers are not conservative in the design features they come
up with. That there has been a gradual (although as yet incomplete)
convergence towards zero-based indexing in languages aimed at
programmers, and one-based indexing in languages aimed at non-
programmers, tells you everything you need to know.
--
Steven
More information about the Python-list
mailing list