Hacker News new | past | comments | ask | show | jobs | submit login

Arrays start at 1. Just... no.



From an aesthetic perspective, I agree. In practice, it wasn't a problem, though. I rarely accessed arrays with a constant index anyway.


maybe I'm being dim but what possible aesthetic benefit does it offer? The only benefit that springs to my mind from a language design perspective is that it is most likely familiar to the user of the language from languages they have used before.

Both Smalltalk and APL use 1 based indices so I just find any claim of obvious aesthetic superiority on this issue a little hard to accept without some back up.

If speaking aesthetics the fact that the number of items in your array is equal to the position of the last index item in the array would seem aesthetically superior.


Lua's 1-based indexing is largely inspired by Fortran. Lua's first users were Brazilian engineers in the early 90s, who were mostly experienced in Fortran.

Arguably, one-based indexing is more closed aligned to mathematics tradition. That point about being easier to index the last element helps to iterate backwards over an array. With zero-based indexing, a backwards loop becomes for(i=N-1; i>=0; i--). Yuck.


exactly, I have a hard time understanding any argument for 0 based indexing other than tradition or a performance hack that I find unlikely was ever that great a benefit anyway.


It is admittedly a mindset that is born of C coding. Pointers and memory offsets and so forth. But lots of languages that abstract such "close to the metal" factors away, still use 0 based indexing.


I meant that aesthetically, I find 0-based indexing more appealing.


I like the arguement that 0-based indexing is better for representing offsets, but 1-based indexing is better for representing actual indices. https://hisham.hm/2021/01/18/again-on-0-based-vs-1-based-ind...


Why? 0 based indexing came from a time where you used memory addresses directly to index into arrays. Today, we don’t do that, so 1 based indexing seems extremely reasonable. Besides, with Lua you can still do 0 based indexing with a small personal extension function.


One of the first things requested by users of an internal media sequencing tool where time is measured by frames was 1-based indexing, they absolutely hated starting from 0


Yea, I love how 0 indexing is such a big deal. Like tabs vs spaces, it doesn’t matter even a tiny bit, it’s like a religion. It’s not that hard to index off of 1, and like I said, you can write like 5 lines of code to make 0 indexing the default!


Very insightful discussion.


Thank you. I'm here all week.. :-)


Nobody starts counting things at 0 in real life so… yes please!


I suppose it's whether you think of array indexing as an operation that offsets the read location relative to th start of the array. It's probably rather low level for the way people are used to working these days.


I think it's just common sense of pointing to the first location and saying "one" ("first element"), "two" ("second element") and so on.


Yeah that's a wart, but it's basically the only wart in an otherwise amazing language.


Ehh, local being a keyword (instead of local default, global only if required) was a significantly bigger gaff. The other would be no nulls in tables. 0/1 based indexing is familiarity only.


Requiring a keyword to declare a variable is a good thing. Otherwise, it'd be easily to accidentally declare new variables when you meant to assign to an existing one. Since global is the default, starting a program with something like this effectively keeps that from happening:

  local gindex = {}
  setmetatable(_G, {__index = function(t,k)
    local v = gindex[k]
    if v == nil then
      error("Bad read of global variable " .. tostring(k), 2)
    else
      return v
    end
  end, __newindex = function(t,k,v)
    error("Bad write of global variable " .. tostring(k), 2)
  end})
  for k,v in pairs(_G) do
    gindex[k] = v
    rawset(_G, k, nil)
  end




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: