Programming silliness.

I started to rant on this a bit in a comment, but changed my mind. Why not make a bigger issue out of it, just for fun?

One of the things that really bugs me about programming, because it’s so damn silly, is starting indexes at 0 instead of 1. Maybe there’s a historical reason for it, but I can’t fathom what it might be. Does this make sense to anyone? Try this:

A: “How many items are there?”
Z: “There are 3 items”
A: “Let’s start with number 0, work through to number 2, and we’ll be done.”

WTF? Nobody thinks like that, ever. Nobody starts counting with 0, because zero is nothing. Yet for some reason, that’s exactly the way you’re supposed to think a lot of the time in programming. How is that useful?

8 thoughts on “Programming silliness.

  1. Thomas M. Edwards

    I don’t know of anyone who thinks in numeric indexes at all. No one *thinks* “1, 2, 3”, they think “first, second, third”. So why have numeric indexes at all? Why not ditch them in favor of natural language indexes?

    The easy answer is because (a) it’s not the programmer who will be running through the indexes, but the computer, (b) the computer *does* count from 0, and (c) any numeric index will be, essentially, arbitrary because it’s not natural language, which is how humans think.

    So, with those in mind, if you’re going to use numeric indexes at all, rather than natural language indexes, which makes more sense?

    I’m not even going to get into the languages who use 1-based indexes and don’t translate those at compile/interpret time into 0-based indexes thereby short changing you an element out of every array that’s created.

  2. Armin Post author

    Sure, people often think “first, second, third”, but they also think things like Box 1, Box 2, etc, and they certainly *count* with “1, 2, 3”, which is how indexes are used. As I said, nobody starts counting with zero.

    (And the computer running through the indexes isn’t the important thing, it’s how the person interacting with the computer gets to think about what the computer is doing. Otherwise, we’d all learn machine code.)

    I completely fail to see how the computer counts from 0. If you create an array, with one item in it, that’s the first item, but it’s item number 0, which is illogical. Perhaps that stems from the offset, which may be 0, but the user/programmer shouldn’t have to think in offsets when they want to use indexes. That’s silly. But I’m wracking my brain, trying to think of an example of the computer counting from zero, and I just can’t think of one–only examples of languages that seem to want you to do that, and even then, it’s usually only with indexes (none of them generally return 0 when asked how many items the 1 item array/collection contains–although that’s muddied a bit by the fact that not everything returns such a value, and instead you may need to ask for the upper bound, which might be 0, or the length, which is unlikely to be 1).

    So, yes, with all that in mind, using 1 as the first index makes much more sense.

  3. Thomas M. Edwards

    ((We’re probably on wrong sides of an unbridgeable gulf here, but….))

    We’re not talking about the count/tally here (a cardinal), however, we’re talking about the index/position of an element in a sequence (an ordinal). They are not the same thing, even in linguistics (natural language), certainly not in computing or mathematics.

    For example: Say there are five boxes. If I ask you how many boxes are there (the count), you’d probably say five. If I pointed to the box in the middle and asked which box that was (the index), you’d probably say the third or the middle, perhaps the center, but not likely three.

    As long as we know that I’m talking about the third box in the sequence, does it really matter if the index to that box is ‘2’ (0-based natural number sequence, mathematic ordinal), ‘3’ (1-based natural number sequence, linguistic ordinal), or ‘third’/’middle’/’center’ (fully linguistic)?

    Which was my original point, if we’re not going to use a linguistic ordinal, which is the easiest for humans, we might as well use whichever remaining ordinal method is most natural for the computer, which is the 0-base ordinal.

    > (And the computer running through the indexes isn’t the important thing, it’s how the person interacting with the computer gets to think about what the computer is doing. Otherwise, we’d all learn machine code.)

    You cannot be saying that you think it’s hard for a human to deal with a 0-based ordinal. A 0-based ordinal is one, *one*, off of the equivalent 1-based ordinal. How is that difficult for a human to deal with?

    So, I have to disagree that the computer running through indices isn’t the important thing. Since either are *trivial* for humans to deal with, seriously, and 0-based indices are more natural for computers, it seems natural that the computer’s preference has significance in this case. Mind you, if 0-based indices were actually hard for humans to deal with I’d probably have to agree with you, but they’re not.

    > I completely fail to see how the computer counts from 0.

    What is the first non-negative integer (for signed integers, the first integer period for unsigned integers), the first mathematic ordinal? Zero (0). That’s what I was talking about.

    > If you create an array, with one item in it, that’s the first item, but it’s item number 0, which is illogical.

    Not liking it does not make it illogical. It’s perfectly logical. It may not be to your liking, but that’s another matter entirely.

    Mind you, I’m not criticizing your personal preference here, although I’m sure it sounds like it. I’m okay with people preferring 1-based indices, lots of people do. If they’re easier for you, or just make more sense to you, or you merely like them better, whatever, that’s fine. However, saying 0-based indices are illogical, when they are not significantly different, let alone more difficult to use, than 1-based indices, and are more natural for computers to boot, just doesn’t make a lot of sense to me.

    ((Like I said at the start, we’re probably on wrong sides of an unbridgeable gulf. Hehe.))

  4. Stefan

    *stoopid grin*

    On a similar note, as you noticed with VB .NET Armin, all the arrays and such have been aligned to start at an index of 0. Hence this entry.

    However… Collections are an exception. They still start at 1!

    I wish the people who wrote the language were consistant…

  5. Armin Post author

    I started to type out a really long reply, but decided against it. Instead you’ll get this still long, but slightly less so, reply, where I don’t bother to respond to all your points because they’re largely all based on the same assumptions.

    I think you’re right, and we’re on opposite sides of a large gulf here. I’m applying logic from a human interaction perspective, and you’re applying it from a mathematical perspective, and the two can both be logical from their perspectives and not come even close to meeting.

    Of course, you have to go out to the esoteric world of set theory, which is Greek to most humans anyway, to make your argument. You and I both know that, given your five boxes, if you ask what number the selected middle (third) box is, it’ll be 3, not 2. Humans equate the cardinal numbers to the ordinal positions. Regardless of what you look at in set theory, first will be one, third will be three, etc. When you’re using convenient numbers to reference location, humans use the cardinal and the ordinal interchangeably. How they’re defined in set theory is not relevant to actual usage by people.

    Basically, what I’m saying is that *all* high level languages are interpreted, massaged, and spit out by the compiler into something that makes sense to the computer. There is no reason they can’t do something as simple as making things a bit more natural for the people who use them. (And, dude, 0 is both the first non-negative number, and the first non-positive number; it’s neither positive nor negative. But the computer doesn’t care, it’s all just bits that are on or off to it.)

    And yes, I *am* saying that 0-based is confusing (not necessarily hard), for *normal* people. I still remember my early programming classes, so I *know* this is the case. Back then, the hardest thing for them to get used to (beyond grasping arrays at all, which seemed strangely hard for many) was that the third item in the array was index 2, not 3.

    Nowadays, it’s even worse, because (in VB at least) there is a mix of 0-based and 1-based, often times which you’re using in the same routines at the same times. Arrays, 0-based; list boxes, 0-based; collections, 1-based, string character positions, 1-based. If 0-based was so logical, all of these would be 0-based. (Maybe they’re all 0-based in C or other languages, in which case that would be a plus for them, certainly. Consistency is good. I don’t recall, because the last time I used C, DOS was the big OS.)

    So, to wind down, yeah, I’m not sure we’re going to agree. And I guess I can live with that 😉

  6. Armin Post author

    Stefan, looks like we cross-posted. Yes, lack of consistancy is the big hobgoblin in this, I’d say. It’d be much easier to deal with the silliness 🙂 of 0-based if it was *all* 0-based.

  7. Armin Post author

    I forgot to mention this:

    I realized last night that the reason for 0-based in computer science most likely stems from the earliest computers and programmers, and has nothing to do with set theory or anything else. It likely had to do with memory space. Early computers were so crunched for space that even saving one bit was important, so using 0 instead of 1 might allow you to save that important bit.

    I haven’t done any research to back that up, but it seems likely given what I recall learning about those early days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.