I have to read lines from a text file in sequential order. The file is a custom text format that contains sections. If some sections are out of order, I would like to look for the starting of the next valid section and continue processing.
Currently, I have some code that looks like this:
for (int currentLineIndex=0; currentLineIndex < lines.Count; currentLineIndex++ )
{
//Process section here
if( out_of_order_condition )
{
currentLineIndex--;//Stay on the same line in the next iteration because this line may be the start of a valid section.
continue;
}
}
Is this code smell?
UPDATE: I didn't mention this earlier, but the root cause of this kind of code was a complicated switch-case (typical when you're parsing).
I got rid of the incrementing/decrementing variable by using the "goto case" statement.
The structure now looks like this:
switch(state)
{
case State.BOF:
{
//Process BOF case
}
case State.SeenHeader:
{
if( out_of_order_condition )
{
state = State.BOF; //Reset the state to some respectable one
//currentLineIndex--; Removed
//continue; Removed
goto case State.BOF;//Handle this in this iteration itself.
}
}
}