this post was submitted on 19 Sep 2024
1482 points (98.4% liked)

Programmer Humor

32443 readers
1877 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 month ago* (last edited 1 month ago) (11 children)

That's because the word "first" in first() uses one-based indexing. In true programmer fashion it would have been called zeroth() but that is wholly unintuitive to most humans.

I maintain that the element with the lowest index is called the "zeroth" element in zero-based indexing and "first" in one-based indexing. The element with index N is the Nth element.

[–] [email protected] 2 points 1 month ago (4 children)

Most humans wouldd never write the word first followed by (). It absolutely should have been zeroth(), and would not cause any confusion amongst anyone who needed to write it.

[–] [email protected] 7 points 1 month ago (3 children)

It absolutely should not have been named zeroth() because the reasoning for that is purely pedantic and ignores WHY arrays are 0 indexed. It's not like the people in the early days of writing programming languages were saying "the zeroth item in the array" - they would refer to it using human language because they are humans, not machines. Arrays are 0 indexed because it's more efficient for address location. To get the location in memory of an array item, it's startingAddress + (objectSize * index). If they were 1 indexed, the machine would have to reverse the offset.
Function/Method names, on the other hand, should be written so as to make the most sense to the humans reading and writing the code, because the humans are the only ones that care what the name is. When you have an array or list, it's intuitive to think "I want the first thing in the array" or "I want the last thing in the array)," so it makes sense to use first and last. That also makes them intuitive counterparts (what would be the intuitive counterpart to "zeroth"?).

[–] [email protected] 2 points 1 month ago (1 children)

Function/Method names, on the other hand, should be written so as to make the most sense to the humans reading and writing the code

Of course—that’s why we have such classics as stristr(), strpbrk(), and stripos(). Pretty obvious what the differences are there.

But to your point, the ‘intuitive’ counterpart to ‘zeroth’ is the item with index zero. What we have is a mishmash of accurate and colloquial terms for the same thing.

[–] [email protected] 1 points 1 month ago

explode('brain', 'ai')

load more comments (1 replies)
load more comments (1 replies)
load more comments (7 replies)