Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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 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 1.
- 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.
- 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 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.
- 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.
Dijkstra is Wrong and So am I was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.