Dijkstra is Wrong and So am I

Written by mark | Published 2018/09/15
Tech Story Tags: javascript | programming | arrays | mathematics | computer-science

TLDRvia the TL;DR App

I repent for using 0-based Arrays, I was wrong. Let me share with you why.

Programmers ought to use index notation, instead of offset notation. Offset notation is the common practice of describing the position of an element in an array by its corresponding location in the physically allocated space of contiguous memory, which logically starts at a zeroth initial. This is otherwise shortened to “0 based index arrays”, despite the misnomer of it actually being an offset. We should instead use 1 as the beginning of an Array, because it offers the following advantages:

  1. Naturally, the first element in a list cardinally corresponds to 1. Contrarily, even official documentation of JavaScript has explicit disclaimers that the "first element of an array is actually at index 0" - this is easily forgotten, especially by novices, and can lead to errors.
  2. Mathematically, a closed interval is properly represented in code as for(i = 1; i <= items.length; i++), because it includes its endpoints. Offset notation instead is technically a left-closed right-open interval set, represented in code as for(i = 0; i < items.length; i++). This matters because code deals with integer intervals, because all elements have a fixed size - you can not access a fractional part of an element. Integer intervals are closed intervals, thus conclusively proving this importance.
  3. Mathematically, matrix notation also starts with 1.
  4. What should the 0th item represent then? How about the Array itself? Especially for languages that treat 0 as a falsey value, if 0 || true gives you true, it isn’t unreasonable to think that foo[0] gives you the root of foo, which is the Array itself.
  5. The last element in a list cardinally corresponds to the length of the list, thus allowing easy access with items.length rather than having frustrating (items.length - 1) arithmetic everywhere in your code.
  6. Negative indices are symmetric with positive indices. Such that -1 and 1 respectively refer to the last and first element, and in the case where there is only one item in the list, it matches the same element. This convenience allows for simple left and right access that offset notation does not provide.
  7. Non existence of an element can be represented by 0, which would conveniently code elegantly as if( !items.indexOf('z') ) return;. Rather, one must decide upon whether if( items.indexOf('z') == -1 ) return; is philosophically more meaningful than if( items.indexOf('z') < 0 ) return; with offset notation despite ignoring the asymmetry of the equation.

Therefore, I ask forgiveness for all the years of 0-based Arrays I have naively, foolishly, and mistakenly pushed on everyone. Please forgive me, repent with me, and now rise up and rebel! Share, tweet, HN/Reddit, etc. the movement, and let’s Make Arrays Great Again! No, I’m not trolling, the math has spoken.


Written by mark | http://gun.eco Founder
Published by HackerNoon on 2018/09/15