Hacker News new | ask | show | jobs
by airstrike 2918 days ago
I'm not a Lua user, so I thought I'd ask why one-based indexing is a flaw. As long as it's consistent, does it really matter?

As someone who is forced to use VBA, it sounds like Lua could be worse

5 comments

I have spent limited time with Lua - mostly just fiddling with pico-8. My experience has been that it's fairly easy to get used to, but occasionally annoying. For example, it doesn't interact nicely with modular arithmetic the way 0-based indexing does.
If you store a 2d array in a 1d one, it's natural to try/use x+200*y or such. With 0-based indexing it just works. With 1-based indexing, it's more complicated. If you do any modulus-based array access, 1-based indexing makes it more complicated. Basically, a world with 0-based indexing results in simpler math than a world with 1-based indexing, making it the better choice.
Because Lua is often used as an extension language of C++ which has 0-based indexing: it's really painful for programmers to switch from one indexing to another.
If x is some object then x[k] is intuitively some object at distance "k" from the beginning of x.

So the object right at the beginning is x[0].