3 ms·
a > b is the same as b < a and I would argue a > b is clearer in this instance by noting the symmetry of the following line: if ( a[smallestSoFar] > a[next
by jjaredsimpson 11y ago
a > b is the same as b < a and I would argue a > b is clearer in this instance by noting the symmetry of the following line:
if ( a[smallestSoFar] > a[nextToCheck] ) {
smallestSoFar = nextToCheck ;
}
When a > b then a = b
The order of the variables doesn't change.
The value is called nextToCheck because the article is specifically talking about loop invariants. The value of nextToCheck only makes sense when loop invariant is established prior to the loop and after work has been done in the loop to reestablish the loop invariant.
In both of those cases the value in nextToCheck is clearly meant to denote the half open interval [0, nextToCheck) which contains the smallestSoFar.
currentComparisonIndex is not the correct name for the loop variable because that name is only accurate after checking the termination condition and prior to incrementing. This is not what the loop invariant states the purpose of nextToCheck is. Therefore you would not have established a proper loop invariant prior to entering your loop. nextToCheck might more fully be called 'nextToCheckAfterTerminationCheck'
Of course this is all meta above the level of the language is self. You could call the variable 'foo' and of course the code would still function.
- justinsaccount 11y agoIt must be a mathematician thing. When I am doing something that involves finding a smaller number, I expect to see a < operator. How would this look if you were writing code that dealt with something like a linked list where you have 'cur' and 'next' pointers? 'cur' would be 'next' and 'next would be 'nextNext' ?
- GFK_of_xmaspast 11y agoAs a mathematician, that code snippet seems perfectly fine?
- justinsaccount 11y agoI meant that as in, I am not a mathematician. When I am looking at code that is trying to find a smaller number I expect to see a < operator.