3 ms·
> Can someone show me a use case other than trying to cycle over the same data (EDIT: or range) repeatedly where 0-based is fundamentally better than 1-based? I
by xdavidliu 6y ago
> Can someone show me a use case other than trying to cycle over the same data (EDIT: or range) repeatedly where 0-based is fundamentally better than 1-based? I mean, that's the only case I can think of where 0-based is actually going to be easier and more sensible.
Perhaps the canonical treatise on this subject:
"Why numbering should start at zero". E. W. Dijkstra, 1982.
https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
- Jtsummers 6y agoI actually had that in my first draft of my comment. But I hadn't read it in a while and just did. While he endorses 0-based in one of the remarks, he's actually endorsing the notational format of: a <= x < b or [a,b) Where if b is renamed N, the length of a vector/array/list, in a 0-based notation, then you'd describe a 0-based vector's range as [0,N). The reasons he gives for preferring this notation for ranges (not, strictly, for 0-based ranges, but for all ranges): 1. Experience at Xerox where this notation (versus the other 3 he describes) leads to fewer errors. An informal study but a study none the less. 2. Using either [a,b) or (a,b], the size of the range is the difference between the provided bounds. 3. Using [a,b), adjacent ranges can be detected where b_1 = a_2. Given ranges [2,13) and [13,20) you can see that they're adjacent by just comparing two values. This certainly makes it quick to visually inspect as a code reader/writer. (the same argument can be made for (a,b]) 4. An argument for either [a,b) or [a,b] is that the lower bound should be described by the minimum number in the range because it's more aesthetically pleasing. So by process of elimination, he's left us with [a,b) as the better notation of the 4 options. Based on an aesthetic argument, if you accept the above, then 0-based makes more sense because [0,N) is more aesthetically pleasing than [1,N+1). But if you use notation (c) from his report: a <= x <= b or [a,b] Then 1-based can be described as the interval [1,N] where N is both the last element and the length of the vector/array/list. Which seems rather pleasant/natural to my eyes and fingers as well. ---------- If we accept the experience at Xerox, then his argument for 0-based indices is reasonable based on the assumption that ranges should be described as [a,b). If we don't accept it, then his argument is mostly based on aesthetics. That is, it's more pleasant to do a computation like: range size = b - a than (for ranges described with [a,b]): range size = b - a + 1 And it's more pleasant to do a comparison like: adjacent? b_1 = a_2 than (for ranges described with [a,b]): adjacent? b_1 = a_2 - 1 But that first case doesn't matter in a 1-based array because the range size is just `b`, it's already stated in the range and there's no need for computation (just as it's present in 0-based ranges). Now, if your language permits arbitrary ranges then I think a case could be made for his suggested [a,b) notation. But if you're only choosing between 0-based or 1-based, I don't find it persuasive. It's still a tossup for me, neither is better than the other unless you also choose his notation for describing ranges, where [1,N+1) would be awkward but [1,N] is easier to use and understand.
- alentist 6y ago> Then 1-based can be described as the interval [1,N] where N is both the last element and the length of the vector/array/list. Doesn't work when the start position isn't 1. > But that first case doesn't matter in a 1-based array because the range size is just `b` Only if a = 1.