The reason I am asking this is because I am doing some analyses on my code by the build in code analysis of my IDE and I get the remark
while statement has empty body at line
Which seems to indicate that having a loop with no lines in them is not a preferred way to do something. I got this code, in which what I do a conditional check which also changes my code, and it works:
while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
}
lastSucceedingIndex--;
Information about what the loop does and why I decided to use it like this: I am using shortcircuit to quit the loop if the first part of the conditional check does not evaluate to true. The second part increases first the lastSucceedingIndex
and then checks if it matches some key
, more often than not this is the case. Since I increase lastSucceedingIndex
within the header I leave the while loop empty. To later correct for the off by one bug I add the last line after the while loop.
Now my question is, is having an empty loop good practice? Or should I move the lastSucceedingIndex to the body and increment it there? Or is there even a third option I am missing?