3

I've got this 3 piece of code:

 For x = 0 To sections.Length - 1
        'work
    Next
    For x = 0 To UBound(sections)
        'work
    Next
    For x = 0 To sections.GetUpperBound(0)
        'work
    Next

I've been thinking for about 10 minutes but I still can't decide what exactly are the advantages of 1 choice over the other.

I was wondering if anyone has actually given this some thought and would like to share their insights. Basically I would like to know which is "better" (you can quote clarity, easy refactoring, performance, maintainability, good standard, anything that makes it "better")

Pacerier
  • 86,231
  • 106
  • 366
  • 634

3 Answers3

4

I'm assuming you need to use the index within the loop - otherwise, just use a For Each loop instead.

1 is the idiomatic way for a single-dimensional array, in my view. It's the closest to the normal way you'd see in C#:

for (int x = 0; x < array.Length; x++)

It's unfortunate that VB has this slight dual personality these days in terms of whether it's zero or one-based. .NET is generally a zero-based platform, and you're suffering for it here; languages designed around zero-based APIs are likely to make the "inclusive lower-bound; exclusive upper-bound" pattern simpler - whereas VB is inclusive at both ends, which is why you need the "-1".

2 looks like it's a VB-specific function; I would try to avoid these unless they present significant advantages, as it will make it harder for non-VB programmers (e.g. C# programmers) to understand the code. That's only a very minor concern in many cases, but when there are no real advantages to it, I'd stick with "regular" .NET approaches.

3 is really designed for multi-dimensional (rectangular) arrays - the 0 there is saying that you want the first dimension. When you're dealing with single-dimensional arrays it's adding extra complexity for no good reason.

It's possible that the first approach is also the most performant - the JIT recognizes this pattern and is able to remove some array bounds tests within the loop (it knows that x is within the bounds of the array). Now UBound may compile to IL that has the same property - I'm not sure. I suspect it makes a call to a method within the Microsoft.VisualBasic assembly, which the JIT is less likely to have knowledge of.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • #2 [Ubound](http://msdn.microsoft.com/en-us/library/95b8f22f(v=VS.90).aspx) is indeed a VB-specific function, included in VB.Net for compatibility with earlier versions of Basic like VB6. [There](http://stackoverflow.com/questions/2426664/vb-net-vs-framework) [are](http://stackoverflow.com/questions/1485712/is-use-of-mid-instr-lbound-ubound-etc-in-vb-net-not-recommended) [other](http://stackoverflow.com/questions/226517/is-the-microsoft-visualbasic-namespace-true-net-code) [questions](http://stackoverflow.com/questions/241822/vb-runtime-functions-in-vb-net-for-vb6-programmers) which discuss wh – MarkJ May 18 '11 at 08:29
  • *drat the character limit!* ... other questions which discuss whether these VB-specific functions should be avoided – MarkJ May 18 '11 at 08:34
1

It basically depends on the semantics of what you are doing. Choose whichever one most clearly expresses what you're doing in the code.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1

I prefer the third one because it's most clear what you're measuring - you're getting the upper bound of the first index of the array "sections". The others describe the same thing, but I don't think they do it as clearly.

SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • 1
    In the common case of a single-dimensional array, explicitly specifying a dimension feels like additional complexity for no reason to me. I use multi-dimensional arrays so rarely that I believe handling those should be the exception rather than the rule. – Jon Skeet May 18 '11 at 06:11