Question

I have a program that loops over a list of vehicle co-ordinates and then performs other functions on them. I am only looking for pairs of successive co-ordinates that are not too close together. If a vehicle is stationary for a long time, there is the potential for thousands of successive co-ordinates to be on top of each other.

Once I find my two co-ordinates, I am manually updating my iterator (i=j-1) so that I don't have to go over the many thousands of co-ordinates repeatedly. My question is, is this good practice? Or is there a viable alternative? I could not find any examples of this online. I saw the 'continue' statement, however this seemed like it would require an extra if statement, and the manual update was more 'elegant'.

int LatLngSize = latLngList.size();
for (int i = 0; i < LatLngSize; i++) {
    j = i + 1;
    validPoints = true;
    if (LatLngSize > j) {
        latLng1.setCoordinate(latLngList.get(i));
        latLng2.setCoordinate(latLngList.get(j));
        consecutivePointDistance = latLng1.distance(latLng2);
    }
    while (consecutivePointDistance < 0.05) {
        j++;
        if (LatLngSize > j) {
            latLng2.setCoordinate(latLngList.get(j));
            consecutivePointDistance = latLng1.distance(latLng2);
            i = j - 1; // This is the potential offender.
        } else {
            validPoints = false;
            consecutivePointDistance = 100;
        }   
    }

    //Do stuff with my latlng1 and latlng2
}
Was it helpful?

Solution

Yes. It is bad practice. It will compile and run just fine, but in answer to your question, it is bad practice.

If you want to manually update your iterator, use a while loop.

Anything you can do with a for loop can also be done with a while loop. The choice in what loop to actually implement is a matter of readability. In for loops, people expect the iterator to be updated in the update statement and only in the update statement.

If you can't figure out how to rewrite your loop so your iterator is updated only in the update statement, consider something like this:

for(int i=0; i<someValue; ++i /*i also updated at line 18*/)

Just as an example. This is still worse than rewriting the code so it's not updated at line 18 (or whatever line) but far better than updating it at line 18 and not leaving the comment here in the update statement.

Alternatively, per comments on this answer, try this:

for(int i=0; i<someValue; /*see body*/) {
    //do stuff
    //update i
    //do stuff
}

Now in this case, your update statement is completely empty, so even without the /*see body*/ comment, anyone maintaining your code already know that i must be modified somewhere within the body. And the ++i behavior from the update statement can be recreated simply by adding ++i as the last line of the for loop.

OTHER TIPS

Yes. It's a bad practice. Don't do it. If I'm maintaining your code, and I see

for (int i = 0; i < latLngSize; i++)

I take this to mean that i increases by one, over and over, from 0 until it reaches latLngSize. I don't look further for other instances of i changing. So, change i at your peril, and only if you don't mind incurring the wrath of all future developers.

Doesn't this do what you want?

int size = latLngList.size();
for (int i = 0; i + 1 < size; i++) {
    latLng1.setCoordinate(latLngList.get(i));
    latLng2.setCoordinate(latLngList.get(i+1));
    if(latLng1.distance(latLng2) >= 0.05) {
        //Do stuff with my latlng1 and latlng2
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top