{"id":470,"date":"2011-01-13T17:20:14","date_gmt":"2011-01-13T22:20:14","guid":{"rendered":"http:\/\/alarmingdevelopment.org\/?p=470"},"modified":"2011-01-13T17:30:36","modified_gmt":"2011-01-13T22:30:36","slug":"why-numbering-should-start-at-one","status":"publish","type":"post","link":"https:\/\/alarmingdevelopment.org\/?p=470","title":{"rendered":"Why numbering should start at one"},"content":{"rendered":"<p>Should collection\/sequence\/array indices start with zero or one? Most current languages choose zero. For\u00a0<em>flux,<\/em> I am choosing one. This choice is orthogonal, meaning that I can easily change it if it turns out to be wrong. The reason to discuss such a trivial issue is that it is an example of how choices that made sense in the early days of programming need to be reexamined. It also frames some principles of language design: Abstract Datatypes, and Conservatism.<br \/>\n<!--more--><\/p>\n<p>My goal is to make programming easier, so I want to reduce cognitive effort wherever possible. Human culture, language, and even mathematics have a deeply entrenched convention that you count things starting with one. We say something is the &#8220;first&#8221;, not the &#8220;zeroth&#8221;. Mathematicians describe sets as {x<sub>i<\/sub>}<sub>i=1..n<\/sub> not {x<sub>i<\/sub>}<sub>i=0..n-1<\/sub>. Novice programmers find zero-based indexing surprising at first. Using different conventions in different contexts is not just a one-time learning cost, but a continual cognitive cost. I always have to remember to subtract one from the length to get the last element. These little bits of wasted attention are small, but add up, and we can&#8217;t afford to waste any. <\/p>\n<p>I take it as a principal of programming language design that entrenched conventions should be contradicted only for compelling reasons. Call this principle Conservatism. Assembly Language and C have a good reason: zero-based indexing is convenient for pointer arithmetic. But there is no pointer arithmetic in a sane high-level language. So why is zero-based indexing so prevalent?<\/p>\n<p>Dijkstra <a href=\"http:\/\/www.cs.utexas.edu\/users\/EWD\/transcriptions\/EWD08xx\/EWD831.html\">argues <\/a> for zero-based indexing. His argument is in two parts. First, he argues that intervals of integers should be specified as: <em>first <\/em>&#8804; i &lt; <em>last<\/em>+1. He then argues that the indices of an array are &#8220;nicer&#8221; as 0 &#8804; i &lt; <em>length<\/em> rather than 1 &#8804; i &lt; <em>length+1<\/em>.<\/p>\n<p>Ladies and gentlemen of the jury, Dijkstra is committing <strong>representation exposure<\/strong>! To be fair, we all did that back then. He is assuming an integer interval <em>is <\/em>a pair of integers. That may be its internal representation, but that is irrelevant to language design. We need to consider what should be the abstract interface to the interval datatype. An interval should have at least the properties <em>first<\/em>, <em>last<\/em>, and <em>length<\/em>. We can construct an interval by supplying any two of these properties, and there should be literal syntax like <em>first<\/em>..<em>last <\/em>and <em>first<\/em>++<em>length<\/em>. Maybe there is also a <em>next <\/em>property one greater than <em>last<\/em>, and maybe the literal syntax should be <em>first<\/em>..<em>next <\/em> as Dijkstra wants. Regardless, once we have properly abstract intervals, the choice of initial index becomes arbitrary. The range of indices of an array of length <em>n <\/em>can equally well be either 0++<em>n <\/em>or 1++<em>n<\/em>.<\/p>\n<p>The point here is that by properly abstracting intervals, we can be precise about the distinction between indices and lengths, and encapsulate the arithmetic to relate them. All these +1 and -1 operations are really hard-wired conversions that should be abstracted away. I conjecture that the lack of such abstraction is why fencepost errors are so common.<\/p>\n<p>Thus, without any compelling reason to choose zero-based indexing, the principle of Conservatism tells us to use one-based indexing. Unless we value similarity to existing programming languages more than to natural languages, in which case the same principle tells us to do the opposite. But in either case it is an arbitrary convention. It is high time for us to get over Assembly Language. I would go even further &#8211; I think some of the enthusiasm for zero-based indexing stems from intellectual vanity. &#8220;Look at me: I&#8217;m so smart I count differently from normal humans.&#8221; Such self-defeating cleverness is a plague upon our profession.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Should collection\/sequence\/array indices start with zero or one? Most current languages choose zero. For\u00a0flux, I am choosing one. This choice is orthogonal, meaning that I can easily change it if it turns out to be wrong. The reason to discuss such a trivial issue is that it is an example of how choices that made &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/alarmingdevelopment.org\/?p=470\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why numbering should start at one&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfEnU-7A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=470"}],"version-history":[{"count":52,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":523,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/523"}],"wp:attachment":[{"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alarmingdevelopment.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}