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
- 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.
- Mathematically, matrix notation also starts with
- What should the
0th item represent then? How about the Array itself? Especially for languages that treat
0as a falsey value, if
0 || truegives you true, it isn’t unreasonable to think that
foogives 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.lengthrather than having frustrating
(items.length - 1)arithmetic everywhere in your code.
- Negative indices are symmetric with positive indices. Such that
1respectively 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 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.