966
you are viewing a single comment's thread
view the rest of the comments
[-] JackbyDev@programming.dev 6 points 4 months ago

Arrays not starting at 1 bother me. I think the entrenched 0-based index is more important than any major push to use 1 instead, but if I could go back in time and change it I would.

[-] Overshoot2648@lemm.ee 16 points 4 months 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@lemmy.world 8 points 4 months ago

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

[-] thedeadwalking4242@lemmy.world 5 points 4 months ago* (last edited 4 months ago)

For i = 0; I < array.length; i++

[-] xthexder@l.sw0.com 6 points 4 months ago

i < array.length or else you overflow.

[-] IndustryStandard@lemmy.world 3 points 4 months ago

Casually throws in capitals as well.

[-] thedeadwalking4242@lemmy.world 2 points 4 months ago

My post is a work of fiction

[-] dan@upvote.au 3 points 4 months ago

A lot of languages have a .last() or negative indexer ([-1]) to get the last item though.

[-] Klear@lemmy.world 3 points 4 months 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@programming.dev 2 points 4 months 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.

[-] Username@feddit.org 4 points 4 months 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@programming.dev 1 points 4 months 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.

[-] Hawk@lemmynsfw.com 2 points 4 months 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.

[-] chuckleslord@lemmy.world 4 points 4 months ago

Also remove null reference

[-] Pika@sh.itjust.works 1 points 4 months ago

this is what messed me up with ZSH for a bit, having a shell default to 1 instead of 0 was weird

[-] JackbyDev@programming.dev 1 points 4 months ago
this post was submitted on 11 Feb 2025
966 points (96.5% liked)

Programmer Humor

24174 readers
2492 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS