1

It is very, very common to see code like this:

for (int i = 0; i < array.Length; i++)
{
    DoSomething(array[i]);
}

The above code makes certain assumptions about arrays (which apparently hold true most of the time, but not all the time). Wouldn't it be more explicit and more forward-compatible to use something like this instead?

for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
    DoSomething(array[i]);
}

Why is the former format so widely accepted and used?

(I know we could use foreach, but let's assume that there is some reason that would not work for us in this specific case).

John Wu
  • 26,032
  • 10
  • 63
  • 84
  • Related https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm – Kevin Fee Jan 11 '17 at 00:10
  • 1
    If you are iterating through the array, in c#, you should be using the foreach construct which is specifically for that purpose. You'll need to describe why that's not available otherwise we are speculating about an alternate universe. – whatsisname Jan 11 '17 at 00:50
  • 1
    `foreach` is not always appropriate, e.g. if working with parallel arrays, or if adding and removing items from the array. – John Wu Jan 11 '17 at 00:51
  • It is probably convenient to think of arrays as going from 0, 1, 2, ..., N-1. The first version reflects this thought process. Also most languages make this choice already and don't let you change it. Even in C# you could always as a team have a style rule that says not to use the feature that lets you change array bounds. – Brandin Jan 11 '17 at 08:22
  • 20 years ago, i was arguing (from the POV of DSP) with the creator of MATLAB that all MATLAB arrays (or "matrices" or "variables") should have adjustable index origins. if arrays are made into `structs` (or whatever they call them in C#) with the array bounds tightly attached, then maybe you gotta point. but array indexing should be simply translatable to pointer arithmetic and that is far easier when each dimension of the array has 0 as the origin. [also check out this](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html). – robert bristow-johnson Jan 11 '17 at 09:05
  • These are all arguments for requiring that all arrays start at 0. They do not. So, given that they do not, why is it OK to assume they do? – John Wu Jan 11 '17 at 15:46
  • My opinion is that programming should not need to bend the developer to the underlying architecture. So even though I waste the 0th byte of a for loop I will very often start it at a 1. – SDsolar Jan 12 '17 at 21:14

1 Answers1

1

Your suggested alternative assumes that the array is one dimensional. So, I guess you can't win them all... ;)

Arrays are a relatively primitive data type. I feel free using them internally to the implementation of an abstraction, just as I would using int's and strings. In those cases, the creator of the array and the consumer/user of the array is one and the same class, so the assumptions of zero-based index are natural, easy to verify, and relatively easy to change if there'd be some reason.

I would feel less free using an array as part of an abstraction for a client (even if only me later) to use, just as I feel more uncomfortable making an abstraction in terms of primitive int or string. In the case of creating an abstraction, I'd prefer handing out (or consuming) a dedicated type that represents a collection or an iterator/generator. This to protect both clients and me from the language-specific details of arrays (ints/strings), to create more type safety by introducing well defined types, and to try to move the abstraction up toward the client's domain and away from the implementation domain.

Erik Eidt
  • 33,282
  • 5
  • 57
  • 91