6 ms·
> Complaints about 1-based indexing for arrays I think this misses the point, at least for my case. It doesn't matter which is technically better. What makes
by epage 6y ago
> Complaints about 1-based indexing for arrays
I think this misses the point, at least for my case. It doesn't matter which is technically better. What makes more sense for humans is a matter of application (and even "non-dev" stuff varies as to which is better).
For me, my big concern about using Lua in my products is
- I expect other developers to be writing the Lua scripts
- I expect the developers to be frequently using 0-indexed languages
In this scenario, "being different" means you are likely to run into off-by-one errors. Think of that crufty Perl script you might have around at work where people avoid touching it because no one is comfortable enough with Perl. Having predictable semantics helps a lot in scenarios like this.
- noizejoy 6y agoIt doesn’t miss the point, because the point was that non-developer humans will do better with 1 based indexing. Side note: As a former developer, turned regular human being who scripts from time to time, I don’t find switching between 0 based and 1 based indexing particularly intellectually taxing.
- TulliusCicero 6y agoYeah I have a bunch of complaints about the language that I developed while learning it and simultaneously teaching my 9 year old kid, but the 1-based indexing isn't one of them. That's totally fine, and certainly easier to grasp for a beginner. But there's certainly other stuff that's just weird and bad, and I think just the fact that it's dynamically typed made it harder for him to build an accurate mental model -- sometimes he didn't even realize that an identifier that he had typed in earlier was a variable, let alone what kind of value it'd be holding.
- zajio1am 6y agoWell, i started programming in Pascal (with 1-based indexing) and later switched to C. I remember that after switching to C off-by-one errors magically disappeared.
- billfruit 6y agoWait till you need to use a formula which has somewhat complex uses of indexes like 2^(2n+1) and see how having deal with them in 1-base indexing in addition to 0 based indexing makes it add bit of unnecessary confusion.
- craftinator 6y agoIt's not an intellectually challenging context switch, which is actually what makes it such a problem. You don't have to think about it, because it's so simple and easy, so sometimes you won't even think to do it.
- remram 6y agoDoesn't it bother you as the person writing both the application and the original scripts, to be using both styles at once in the same product (sometimes to refer to the same data)? Nevermind the end-users' additions.
- phaedryx 6y agoThere are only two hard things. This is an aside, but I think that "index" is the wrong name. I wish that "index" meant you start counting at 1 (which everyone outside of CS does) and "offset" was the name for starting at 0.
- nvader 6y ago1ndex and 0ffset. I like it.
- Pet_Ant 6y agoI think I will intentionally start using this usage. Maybe we can make it a thing. I mean we already have argument & parameter for similar things to make a distinction.
- zajio1am 6y ago> (which everyone outside of CS does) In set theory (math foundations), ordinals also start at 0.
- deleted 6y ago[deleted]
- layer8 6y agoThat usage gets awkward though when you want to talk about the offset from one array index to another (and the array is 0-based). In actual (common) usage, the distinction between index and offset is the distinction between absolute and relative positions.
- mumblemumble 6y agoSo, I don't do a lot of Lua programming - basically just having fun with Pico-8 on occasion - so I can't say I have a lot of experience with this. Every single other language I use starts at 0. But what I have seen is that, starting from 1 felt weird to me for approximately 1 hour, tops. After that, it was no more or less of a switching challenge than switching between semicolon languages and non-semicolon languages. In other words, no big deal. And, once I got over it, I was pleasantly surprised to find out that I actually have fewer off-by-one errors in Lua than I do in other languages. Because the author is right: No matter how much professional experience I have starting with 0, starting with 1 is still the more innate way of thinking. Also, my single biggest all time source of off by one errors is translating between mathematical notation and code in a 0-based language. That fact alone is cause for me to hope that Julia might one day supplant Python for data work.
- bsder 6y agoI'm glad that's true for you, but my personal experience suggests otherwise. Dijkstra also mentions the Mesa programming language which had all four of the conventions and that anything other than 0-based, half-open leads to errors. See: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E... And for those claiming the world is 1-based, they're full of it. Machining measurement, for example, is ALL 0-based. We're talking a field that has multiple standards for everything and yet they converged on 0-based measurement. The bigger problem, though, is that I even have to use array-based indexing to iterate something. That's just asking for bugs. And, my favorite question for people who like 1-based is "Given index and length N, how do I create index-1?" The answer, of course, is newindex = index - 1 % N. The question is, of course, a trick because the answer is really: newindex = (index - 1) % N. Don't forget the parentheses or you have a bug. Yeah, no. I'll stay in my 0-based languages thanks. Side Note: I have other objections to Lua. The big one being that it's not really compatible between Lua installations. Everybody compiles different modules so Lua becomes neither small nor is it the same language between any two implementations.
- samatman 6y ago
- brundolf 6y agoAgreed. 1-indexing may be more intuitive for humans broadly, and this may have been a good argument before C became ubiquitous, but when it comes to programmers the landscape of what's intuitive is different; 0-indexing is a deeply ingrained habit. Very few norms are so universal across programming languages
- d3ckard 6y agoThat’s more dependent on language syntax IMO. When you have traditional C-style loops, maybe. When you use ranges, iterators etc. it doesn’t make any difference.
- goatlover 6y agoBut it's not universal, since there are established languages like Fortran and R which are 1 based. And Fortran proceeded C.
- berkut 6y agoYep, I think consistency is key here... Especially when it's used as an embedded language in a C/C++ app, or an additional Lua API alongside other APIs that exist already. Having to convert back and forth between 0 and 1 based indices is very annoying, and error-prone. You can make helper methods to abstract some of it, but that only gets you so far, and it's annoying (at least I found it that way) having to context switch your brain depending on what part of the call stack you're looking at.
- chmod775 6y agoNot only that, but starting at 0 just works mathematically better when doing stuff like dividing into round/ceil/floor or a modulo operation. If your index starts at 1 these can become really unintuitive. Depending on what you're doing you'll have to add/subtract 1 after/before operations a lot. The only upside I'm aware of is that the 'length' of your array is now a valid index, which is mostly not very useful unless you want to iterate backwards.
- themadsens 6y agoBrillant. This distinction nails it. Luckily it doesn't matter much in 99.9% of the cases where one just enumerates/filters/maps a collection and indexing is not involved.
- intrepidhero 6y agoI've written a fair bit of Lua in last 5 years and never once cared how arrays are indexed. `for _, value in ipairs(mytable)` works quite well. Looping by index hasn't been necessary.
- CountSessine 6y agoDjikstra argued in favour of 0-based indexing and against 1-based indexing. Let’s just agree that if you’re arguing for 1-based indexing, you’d better have a pretty fantastic argument. I think for FORTRAN or Julia, using 1-based indexing is a good idea, because most mathematical algorithms are written in notation using 1-based indices. As someone who spent a semester debugging subtle off-by-one errors and small differences in results in his scientific computing classes because I foolishly opted for C rather than FORTRAN, understand that converting algorithms with nested summations and integrators from one convention to another is painful. Lua should never have used 1-based indexing because it was meant to interface with C code which itself will have data structures that use 0-based indexing. Writing lua extensions for wireshark, for example, is incredibly painful just because of this.
- samatman 6y agoI was with you up to the last paragraph. Lua uses 1-based indexing because it started as a data description language, and data tends to use 1-based indexing. It evolved to where the primary use case is at odds with this origin. I work with Lua every day and consider the indexing convention to be a genuine flaw. But it's useful to know where that came from, because it wasn't a mistake.
- CountSessine 6y agoThat makes a lot sense. History is a path-dependent function, I guess. Interesting that it was a data description language - in that context, 1-based indexing really would be called for, although maybe flexible indexing like Pascal’s would be even better.
- garaetjjte 6y agoI don't really see how 1-based indexing makes anything better. It just requires sprinkling magic +1 and -1 in various places. Really only reason to use 1-based indexing I see is to make array index arithmetic needlessly annoying, to discourage users from doing any complex operations there..
- pasquinelli 6y agoAren't those +1's and -1's just the mirror images of the -1's and +1's you have to do in a 0 based system? This whole conversation is really silly. 1 based indexing is such trivial detail of lua that I'm not surprised so many words have been spent discussing it.
- kwhitefoot 6y agoWhy do so many languages restrict arrays to 0 or 1 based indexing? I much prefer the way it is done in Basic where you can choose what you like for each array or even better Pascal where you can use any ordinal type (including boolean) as the index and you can define subtypes of the ordinal types to use as index types.