6 ms·
I have a really hard time understanding why people like 0 based indexes. They are a relic of C style arrays that are based on and interchangeable with pointers
by dmz73 2y ago
I have a really hard time understanding why people like 0 based indexes.
They are a relic of C style arrays that are based on and interchangeable with pointers which use offsets that are naturally 0 based.
Use in later languages gives us endless off-by-1 issues and rise to "for 0 to count/len/num - 1" or even better range syntax that is start inclusive BUT end exclusive.
It is a horrible cludge just to support 1970s language perfomace optimization.
Arrays should start and end at whatever start index is required, not at offset 0 of pointer to fist element of array.
- topato 2y agoSlam! Now this guy really knows how to hate on a zero based index!
- coder543 2y agoSome countries consider the 1st floor to be the ground floor, others consider the 1st floor to be the floor above the ground floor, which the formerly mentioned countries consider the 2nd floor… I think 0/1-based indexing is more subjective than simply being a “relic of C” or a “horrible kludge” :P
- zem 2y agoI've been in the US for over a decade and it still occasionally makes me double-take when a room numbered 1xx is on the ground floor
- Benjamin_Dobell 2y agoHang on. Off by one issues are the argument frequently given in favour of zero-based indices, not the other way around. For example, let's iterate through items placing them in 3 different groups; JS: for (let i = 0; i < items.length; i++) { groups[i % 3].push(items[i]); } Lua: for i = 1, #items do table.insert(groups[((i - 1) % 3) + 1], items[i]) end Don't get me wrong. I like Lua, I've made my own IDE for it, https://plugins.jetbrains.com/plugin/14698-luanalysis https://plugins.jetbrains.com/plugin/14698-luanalysis, but this is definitely not an argument in favour of 1-based indices.
- Jerrrry 2y agoYour second example subtracts and adds 1 nearly arbitrarily, which wouldn't be needed if the convention of the 0-index wasn't so widespread.
- School-Cotton 2y agoYou need the first three elements to go into the first group, the next three to go into the second group, and so on. How would you write it?
- binary132 2y agoThat’s not what that loop does, it puts one item into each next group and loops back over the groups after every three items. Really it ought to be a by-three stepped loop over items, inserting each into each group inline: groups[1], groups[2], groups[3] = items[i], items[i+1], items[i+2] If the group count is dynamic, you can just loop over groups instead, and then step through items by #groups, inserting.
- School-Cotton 2y agoFair enough, for some reason I thought it was i/3 and not i%3. Still, I think the point stands.
- Benjamin_Dobell 2y agoIf it is dynamic one of the loops will also suffer from an off-by-one issue. You can't add 1-based indices together like you can zero-based indices. It's also worth noting your solution exhibits similar off-by-one behaviour. The left hand side constants (integer values) do not match the right. It's error prone.
- binary132 2y agosorry, where is the off by one? the code offered is of course only a solution for the fixed-size groups
- teddyh 2y agoHere’s the ultimate authority on why computer languages should count from zero: <https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF>
- thefaux 2y agoYeah, I disagree with Dijkstra on this. And many other things.
- teddyh 2y agoDijkstra, being one of a handful of luminaries in the field of computer science – indeed, he can be said to have created the field itself – can be (provisionally) taken at his word when he claims something. You, on the other hand, being an anonymous user on a discussion forum, will have to present some pretty strong arguments for the rest of us to take you seriously. Your mere disagreement counts for approximately nothing.
- School-Cotton 2y agoI find that argument to be written in a terse "mathy" style that makes it a bit hard to follow. So let me try to restate it in more concrete "programmy" terms. To iterate over an array with "len" elements, it’s most elegant if “len” appears as a loop bound, rather than "len+1" or "len-1". Thus, in 0-based languages we use half-open ranges, whereas in 1-based languages we use closed ranges: // real C for (int i = 0; i < len; ++i) process(array[i]); // C-like language with 1-based indexing for (int i = 1; i <= len; ++i) process(array[i]); But the second is inelegant when len is zero, because 0 isn’t a valid index at all, so it’s weird for it to appear as a bound.
- samatman 2y agoHaving done fairly extensive parsing work in Lua and Julia on the one hand (one-based), and Python, Javascript, and Zig on the other (zero-based), the zero-based semiopen standard makes intervals dramatically easier to calculate and work with. It's really the semiopen intervals which make this the case, but as the Word of Dijkstra makes clear, zero-basis comes along for the ride, to combine semiopen intervals with a one-basis is perverse. Naturally it's true that for collections and naïve indexing, 1-based is more natural. But those are rare places for bugs to occur, while interval calculations are a frequent place for them to occur. Clearly I'm far from allergic to the other standard, but I come down on the side of the zero basis for that reason.
- School-Cotton 2y agoI have a really hard time understanding why people like 1-based indexes! 0 is the smallest unsigned integer in every programming language I know of that supports the concept of unsigned integer. Why shouldn’t an array at the smallest possible index correspond to the beginning of the array? It’s also very natural to think of arr[i] as “i steps past the beginning of arr”. With one-based indexing arr[i] has no natural interpretation that I know of. It’s “i-1 (for some reason) steps past the beginning of arr”. The only reason I can think of to prefer that extra -1 in your formula is just because human languages (at least the ones I know of) work this way — the 42nd element of a sequence, in normal colloquial English, means the one 41 steps past the beginning. But I’m not sure if there is any logical justification for that. I also, despite being American, find the convention used in many countries of numbering building floors starting with zero to be more logical. I’m on the third floor, how many stories up did I travel to get here? Three.
- fuzztester 2y agoIn India too, the floor at the ground level is called the ground floor (probably that is where the name came from), the one above it is called the first floor, and so on. The convention is probably from British colonial times. Also LED floor numbers in lifts (elevators) in India start from 0 for the ground floor, as do the buttons that you press to go to specific floors. Also, Ground Zero. https://en.m.wikipedia.org/wiki/World_Trade_Center_site https://en.m.wikipedia.org/wiki/World_Trade_Center_site https://en.m.wikipedia.org/wiki/Hypocenter# https://en.m.wikipedia.org/wiki/Hypocenter#
- geocar 2y ago> Why shouldn’t an array at the smallest possible index correspond to the beginning of the array? Because then there is no good way to refer to the index before that point: You are stuck using -1 (which means you can't use it to refer to the end of the array), or null (which isn't great either). > every programming language I know of that supports the concept of unsigned integer Surely you know Python which uses a signed integer as an index into their arrays: list[-1] is the last element of a list. If they only used one-based indexing then list[1] would be the first and that would be nicely symmetrical. It would also mean that list[i-1] would NEVER refer to a value after ‹i› eliminating a whole class of bugs. > It’s also very natural to think of arr[i] as “i steps past the beginning of arr.” I think it's more natural to think of arr[i] as “the ‹i›th element of arr” because it doesn't require explaining what a step is or what the beginning is. The exact value of ‹i› matters very little until you try to manipulate it: Starting array indexes at one and using signed indexes instead of unsigned means less manipulation overall. > find the convention used in many countries of numbering building floors starting with zero to be more logical In Europe, we typically mark the ground-floor as floor-zero, but there are often floors below it just as there are often floors above it, so the floors might be numbered "from" -2 for example in a building with two below-ground floors. None of this has anything to do with arrays, it's just using things like "LG" or "B" for "lower ground" or "basement" don't translate very well to the many different languages used in Europe. The software in the elevator absolutely doesn't "start" its array of sense-switches in the middle (at zero).
- fallous 2y agoThere are 360 degrees in a circle, and the first entry is 0 degrees. The first time element of a day is 0:00:00(and enough 0s to satisfy whatever resolution you require). These were not established in the 1970s, and somehow pretty much everyone understands and works quite well with these systems.
- brabel 2y ago> There are 360 degrees in a circle, and the first entry is 0 degrees. To be pedantic, "first" is associated with 1. And a circle does not have a "first" entry, whatever you mean by entry. I think what you're trying to say is that a circle is a continuous arc going from 0 to 360 degrees, but you should recognize that the "starting point" is arbitrary, any point will do, so there isn't really a "first", and that this is not the same as counting because counting is done with natural numbers, which are non-continuous. The problem of 0 VS 1 makes sense only in counting exactly because it's subjective whether you prefer to count from 0 or from 1. Because zero is the absence of anything, I find it hard to start counting from 0 (when you do, your "first" item is actually your zeroth item, and the next item would be the "first"??!), to be honest, despite being completely familiar with doing so since I've used 0-index programming languages my whole life.
- Timwi 2y agoIf you cut up a circle into n slices (maybe you're drawing a diagram on screen), it's vastly more helpful to think of one of the segments as segment 0 because then the start angle of every segment is index*360/n and the two segments whose border is at your initial angle are the first and last. If you start counting segments at 1, your “first” segment would be some way into the circle, and the two segments whose border is at your initial angle would be the last and the second-last.
- brabel 2y agoDon't you see that what you're talking about is a range, not counting?? A range indeed starts at a zero point because if you don't have another point, you just have an empty range: and that's the equivalent of a zero range. When you have two points in a range, then the "first" point ends up being zero, that's correct, because it's being treated as the beginning of it. An "origin" is always associated with zero. It's perhaps incorrect to use the word "first" in this case exactly because it "breaks" how ordinals actually work (i.e. 1=first, 2=second, etc.). A better word is the "initial" point of a range, and the "other" point is the "final" point. Each segment you consider will be a pair [initial, final]. Here's where your mistake comes in: you assume that you can just take each segment and assign it a single number, but instead of using the ordinal numbers which start from 1, you decided to, arbitrarily, take the initial point of the range as your number instead, which is where the zero comes from. Notice that the zero you chose comes from the pair [initial, final], but the zero only makes sense when it's in a pair (alone, zero means the absence of something, i.e. there's no range... I thought that concept was well understood at least by fellow programmers, but apparently not).
- teo_zero 2y ago> [0-based indexes] are a relic of C style arrays I don't think this is true. They exist in other disciplines (maths for instance) that have no relationship with C or other programming languages from the 1970s. > for 0 to count/len/num - 1 I will counter saying that such a for...to syntax is a relic of BASIC. > or even better range syntax that is start inclusive BUT end exclusive I know that your "better" is sarcastic, but I actually find left-inclusive+right-exclusive ranges fantastic. They allow perfect partitioning, easy calculation of lenght, etc. > Arrays should start and end at whatever start index is required I agree. An accommodating language would let you define both lower and upper bounds of an array, instead of its size.
- fuzztester 2y agoIIRC some BASIC(s) I've used in the past had a statement called: OPTION BASE 1 or something like that, to change the starting index to 1.
- jodrellblank 2y agoAPL has ⎕IO←0 or ⎕IO←1 to change the starting index (only between 0 or 1, not arbitrarily). It doesn't apply system-wide so different code blocks/files/modules(?) can set or reset it, and portable code has to either set it or adjust for it. APLCast podcast has an episode mentioning it where they all seem to agree that this is the worst of all worlds, makes sharing code and integrating codebases needlessly bug-prone, and the language picking a single indexing and sticking to it would have been better, even if the choice hadn't gone the way they would have personally chosen.
- fuzztester 2y agoInteresting. Never used APL, though I tried J a little a few times. Yes, that seems bug prone, somewhat like having to have a config file per module.
- ofalkaed 2y ago>They are a relic of C style arrays Doesn't it predate that by a good amount? I would think it is a relic of the EEs who built the digital world, those early languages show a great deal more relation to the bare metal than modern languages. Creating an array whose index starts at 1 just doesn't make sense from the discrete logic point of view, you are either wasting an element or adding in an extra step. But in this day and age how can a language not have ⎕IO ← 0?
- Timwi 2y agoI honestly think that most of the problem arises from the fact that we just culturally start counting at 1 when talking about everyday things. As it stands, we're all used to it that way, and then computers come along and show us that counting from 0 is often more useful. So we adjust, but only for computer programming purposes. If our species had established counting from 0 as the norm right away (element #n is the one that has n elements before it; you think of the number as the number of steps you have to move away from the starting point), then I suspect the reverse would not be true: I don't think anyone would find a situation in which counting from 1 is so much more convenient that it's worth going against the grain of established norm. So in summary, I think we only think of counting from 1 as natural because it's in our culture. And it's in our culture because ancient superstitious humans had an irrational problem with the number 0.
- School-Cotton 2y agoI absolutely agree with you. People want to start with 1 because English (and presumably a lot of other languages) happen to use the word "first" to refer to the first element of a sequence, and not for any logical reason independent of arbitrary human language.
- gatane 2y agoDijkstra said that 0 was better for reasons.
- deleted 2y ago[deleted]
- Rochus 2y agoIt's funny that nearly half of all comments are below your comment. The topic seems to unsettle people much more than a new programming language. This is also another example of how a downvote button is primarily misused in practice. > Arrays should start and end at whatever start index is required That's what you were indeed able to do with Pascal and also Modula-2, but with Oberon, Wirth came to the conclusion, that other index ranges than 0..n-1 were not needed. In his 1988 paper "From Modula to Oberon" he considers it "inessential" and providing "hardly any additional expressive power", but causing "a hidden computational effort that is incommensurate with the supposed gain in convenience". I think, in the end, it is in the eye of the beholder.