How can I get rid of this goto?
24.09.2009 - 22:01 (11 months, 1 week, 2 days ago)
Filed under programming, TrivadisContent, C#
Help! It's late and I don't see a good way to get rid of this goto construct:
private void advanceTheTimeCursor(
Func<DateTime> nextTime,
Action uponSuccessfulAdvancement)
{
loop:
var t = nextTime();
if (t > reference) return;
uponSuccessfulAdvancement();
pastToPresentCursor = t;
goto loop;
}
I kinda like it, but somebody said somewhere gotos are considered harmful...
Comments
while(true) { //do something }If it is, then that should get rid of the goto statement...var t = nextTime(); while(t < reference) { t = nextTime(); uponSuccessfulAdvancement(); pastToPresentCursor = t; }var t = nextTime(); while(t < reference) { uponSuccessfulAdvancement(); pastToPresentCursor = t; t = nextTime(); }previously t was getting updated once we knew that t < reference which could leave us in a state where t > reference. If you get the next time at the end of the while loop it should then behave as before where it will return when t > reference. hope that helps! :)