• @[email protected]
    link
    fedilink
    143 days ago

    It really doesn’t make sense to start at 1 as the value is really the distance from the start and would screw up other parts of indexing and counters.

    • @IndustryStandard
      link
      83 days ago

      It would screw up existing code but doing [array.length() -1] is pretty stupid.

    • @Klear
      link
      English
      43 days ago

      It doesn’t make sense that the fourth element is element number 3 either.

      Ultimately it’s just about you being used to it.

    • JackbyDev
      link
      fedilink
      English
      23 days ago

      Yeah, but if we went back and time and changed it then there wouldn’t be other stuff relying on it being 0-based.

      • @[email protected]
        link
        fedilink
        33 days ago

        It was not randomly decided. Even before arrays as a language concept existed, you would just store objects in continuous memory.

        To access you would do $addr+0, $addr+1 etc. The index had to be zero-based or you would simply waste the first address.

        Then in languages like C that just got a little bit of syntactic sugar where the ‘[]’ operator is a shorthand for that offset. An array is still just a memory address (i.e. a pointer).

        • JackbyDev
          link
          fedilink
          English
          13 days ago

          I know. But in the alternate reality where we’d been using 1-based indices forever you’d be telling me how useful it is that the first element is “1” instead of zero and I’d be saying there are some benefits to using zero based index because it’s more like an offset than an index.

          • @[email protected]
            link
            fedilink
            22 days ago

            A lot of mathematical languages start from 1: R, Julia, Mathematica (and also Lua and Fish).

            I don’t know why, but in, e.g. R, it doesn’t bother me, I get caught by it in Lua all the time.

            I suppose it’s a function of how far the array is abstracted from being pointers to an address that makes it easier to mentally switch.