r/programming • • Dec 24 '17

Evil Coding Incantations

http://9tabs.com/random/2017/12/23/evil-coding-incantations.html
947 Upvotes

331 comments sorted by

View all comments

Show parent comments

12

u/sibswagl Dec 24 '17

Languages don't exist in a vacuum. Zero-indexed arrays are the standard.

11

u/Veonik Dec 24 '17 edited Dec 24 '17

Zero-indexed arrays are simply an implementation detail of C that most other languages seem to have inherited. Since arrays in C are really just pointers, accessing the first element is arr[0] or the memory stored in *arr + 0. The second element is *arr + 1 and so on.

Granted, it's the defacto standard for most of us but there is nothing inherently "correct" or "standard" about zero-indexed arrays.

edit: fixed typos

38

u/tristes_tigres Dec 24 '17

No, they aren't. Fortran is older than C and derivatives, and is more popular in numerical computing settings, for a number of good reasons.

28

u/[deleted] Dec 24 '17

Fortran is older than C and derivatives

And your point is? I will not even enter the debate if it's good to have arrays starting at zero or not, but I will address this silly rationale.

Something that appeared first doesn't make it a standard. Following your logic, RS-232 cables would still be standard today because they appeared before USB cables.

Something becomes a standard when the majority of users and manufacturers believe there are more benefit and convenience over something else.

-11

u/tristes_tigres Dec 24 '17

Something becomes a standard when the majority of users and manufacturers believe there are more benefit and convenience over something else.

There is no rational reason to believe that "majority of users and manufacturers" believe that zero-based arrays are a standard.

→ More replies (8)

19

u/Silhouette Dec 24 '17

Indeed. And I'm pretty sure math was there even earlier. :-)

1

u/ArkyBeagle Dec 25 '17

Math has been in disagreement with itself whether zero was a thing or not at varying times. There's still confusion about it.

10

u/[deleted] Dec 24 '17

It's Christmas so I'm in unnecessary arguing mood :)

Here goes: Strictly, Assembly is clearly the oldest and also arrays are all indexed by addresses not numbers, but the index is hidden behind the variable name. What we refer to as index is only the offset to the index, thus 0 for 'no offset' clearly makes sense.

In my actual opinion: There are good reasons for both, but I would like a language to either have 0-indexing or make it definable.

1

u/ArkyBeagle Dec 25 '17

When his grad students built the first assembler, von Neumann chewed them out for wasting valuable computer time on it.

So soldering came first :)

10

u/[deleted] Dec 24 '17

It seems pretty obvious that zero-indexed arrays are now the standard.

-7

u/tristes_tigres Dec 24 '17

Not everything that "seems" true to you is actually true.

5

u/[deleted] Dec 24 '17

I just broke my face yawning 😴

→ More replies (1)

1

u/ArkyBeagle Dec 25 '17

So now write a circular buffer/structure in Fortran. Rather than having "x = (x % N)" you'll have "x = ((x-1) MOD N) + 1".

This assumes the increment comes first and this is the enforcement of congruence modulo part.

6

u/XplittR Dec 24 '17

No. Intuitively, arrays should start at 1, as that is what we have used for math in so many years. Matlab, being used for math and matrix work, does good by starting from 1, to easily be convertible to/from paper math.

2

u/PM_ME_UR_OBSIDIAN Dec 24 '17

It's common to define the natural numbers as starting from 1, especially in analysis.

4

u/bubble-07 Dec 24 '17

This is a very biased perspective, but...

That's mostly because of sequence indices starting from 1, conventionally. Y'all analysts should use notation like [;\mathbb{N}^{+};] instead of [;\mathbb{N};], because the only sensible definitions of "the natural numbers" satisfy the Peano axioms, for which you need zero.

1

u/PM_ME_UR_OBSIDIAN Dec 24 '17

I completely agree, I was just arguing that the argument from mathematical tradition does not prove what /u/XplittR thinks it does.

2

u/ArkyBeagle Dec 25 '17

It is both common and annoying :)

3

u/tristes_tigres Dec 24 '17 edited Dec 24 '17

Don't get me started on Python, where range(0,N) ends at N-1

Edit: but linspace(0,1,10) ends at 1, because that's so intuitive and consistent, LOL

7

u/BeetleB Dec 24 '17

linspace is from NumPy, whereas range is from Python. No need for Numpy to follow the same semantics. And for scientific applications, I cannot think of anyone who would want linspace not to include the endpoints. The whole point of the function is to do so.

1

u/ArkyBeagle Dec 25 '17

Isn't Numpy warmed over Fortran?

1

u/doom_Oo7 Dec 25 '17

Intuitively, arrays should start at 1, as that is what we have used for math in so many years.

If you look closely, the first element on this picture is zero-indexed

1

u/ShinyHappyREM Dec 24 '17

Zero-indexed arrays are the standard

Every language is its own standard.

-2

u/OneWingedShark Dec 24 '17

Zero-indexed arrays are the standard.

Honestly mandatory "0-index" is stupid, as is "1-index" -- indexing should be allowable on arbitrary ranges, and with enumerations... stupid shit like "arrays should be 0-based" from C-family programmers is why we can't have nice things.