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:
- 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. - 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 asfor(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. - Mathematically, matrix notation also starts with
1
. - What should the
0
th item represent then? How about the Array itself? Especially for languages that treat0
as a falsey value, if0 || true
gives you true, it isn’t unreasonable to think thatfoo[0]
gives you the root of foo, which is the Array itself. - 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. - Negative indices are symmetric with positive indices. Such that
-1
and1
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. - Non existence of an element can be represented by
0
, which would conveniently code elegantly asif( !items.indexOf('z') ) return;
. Rather, one must decide upon whetherif( items.indexOf('z') == -1 ) return;
is philosophically more meaningful thanif( 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.