Re: how do people feel about exit function from loop



Robert Morley wrote:
I think it all comes back to the reason we allow more than 2 or 3 characters for a variable name in the first place. For myself, as a general rule, if I have nested loops, I try to make the index names indicate their function. For example:

For lngRow = 1 To 10
For lngCol = 1 To 10
SomeArray(lngRow, lngCol) = SomeArray(lngRow, lngCol) + 1
Next
Next

But, what is gained here other than superfluous verbosity over
For lR = 1 To 10
For lC = 1 To 10
SomeArray(lR, lC) = SomeArray(lR, lC) + 1
Next lC
Next lR

or, even (my choice in this case as I would _always_ declare indices as Long so the "l" is also redundant)

For r = 1 To 10
For c = 1 To 10
SomeArray(r, c) = SomeArray(r, c) + 1
Next r
Next r

?

Using the longer names adds nothing as the row/column will be evident in context and besides the pita of extra typing for no good purpose it raises the odds of statements not fitting on the single line or being longer than can be seen on the display/editor window. And, the excessive "weightiness" of the array subscripts/indices tends to overpower the array/variable names, adding complexity to parsing the line visually as well.

Also note one other pet peeve of mine that I think is a glaring error in VB/BASIC design -- the omitting of the iterator variable name on the closing Next statement. Less of an issue the shorter the loop is, but should _never_ be omitted imo simply for clarity of intent.

That said, in something not nested, I'll often use a simpler name (unless I'm already using a logical name in other loops), even though when you think about it, the context is nearly the same. For example:

For i = 1 To 10
SomeArray(1, i) = SomeArray(1, i) + 1
Next

But at the risk of inciting another debate over what constitutes style, I believe this is a style choice for each user to decide on their own.

Certainly it is a style choice but I think there are reasons for selecting certain styles that far transcend the apparent minimal benefit...

I'll close by noting the parallel to mathematical notation of "x sub i" should not be disregarded with impunity. Being, of course, an engineer as opposed to a software professional and heavily involved in modeling and such as the primary applications, I am far more influenced in that regards than many VB users. Still, the precedent is there and should be recognized as having a long and distinguished lineage.

Again, imo, $0.02, etc., ...

--
.



Relevant Pages

  • Re: Sams teach yourself php in 24 hours - book
    ... >> arrays, loops, if/else and functions that's about it. ... >Putting it all into the context of OO code is the next big learning ... Prev by Date: ...
    (uk.net.web.authoring)
  • Re: eof() is there a better way
    ... | I am always having trouble with the following: ... // Meaning while 'true' in an boolean context... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: pass by reference
    ... In English, it's "it". ... Yes, in the context of for loops*, map, and such, from what I understand ...
    (comp.lang.perl.misc)