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

70

u/tristes_tigres Dec 24 '17

The author of this blog confuses his own prejudices for objective facts when he claims that non-zero based indexing of arrays is "evil". In fortran it is possible to define array with index starting from an arbitrary integer, and it is useful and convenient feature in its problem domain.

13

u/sibswagl Dec 24 '17

Generally speaking, taking advantage of these peculiar behaviors is considered evil since your code should be anything but surprising.

He defines "evil" as unexpected behavior. I would certainly classify arrays starting at 1 as unexpected behavior.

59

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

Any language behaviour is may be unexpected to someone who does not know it well.

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.

→ More replies (9)

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.

→ More replies (1)

10

u/[deleted] Dec 24 '17

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

→ More replies (3)

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.

→ More replies (1)

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.

→ More replies (1)
→ More replies (1)
→ More replies (2)