Let me be clear: I am not making this up.
Let me be clear: I am not making this up.
My code base contains something akin to the following:
i := 1;
while i < 4 do
begin
case i of
1: DoSomething;
2: DoMore;
3: DoEvenMore
inc(i)
end;
Repeat: I. am. not. making. this. up.
My code base contains something akin to the following:
i := 1;
while i < 4 do
begin
case i of
1: DoSomething;
2: DoMore;
3: DoEvenMore
inc(i)
end;
Repeat: I. am. not. making. this. up.
State machine gone wrong?
ReplyDeleteEric Grange I have no idea. None -- totally bizarre. That kind of thing is in more than one place.
ReplyDeletePaid by LOC?
ReplyDeletemaybe to be sure that you'd never multithread that code...
ReplyDeleteAre DoSomething/DoMore/DoEvenMore local procedures that modify the value of i?
ReplyDeleteMartin Wienold Nope. It's as bad as you think.
ReplyDeleteSeriously, what kind of programmer would use Magic Numbers like that?! Also, where's the sense in using While instead of For? :-) Sometimes these code samples go a bit over my head, being an amateur programmer and all, but this one's absolutely brilliant.
ReplyDeletemay be some time ago it looked like
ReplyDeletewhile i < 40...
case (i mod 3) of ?
Sometimes, working under high stress or very short time constraints tends to force people to produce such code. It's not uncommon where I come from.
ReplyDeleteI think the procrastination module in my brain contains code like that.
ReplyDeleteSimon Stuart No, I am pretty sure it looks a lot like Nick Hodges's example. The more unpleasant the task, the more "other stuff" that ends up getting done, none of which actually needs doing. It gives me an additional excuse not to do what I am supposed to be doing, once I am completely exhausted.
ReplyDeleteSimon Stuart So was I...and truthful at the same time.:-P
ReplyDeleteNow, should I start working on getting my taxes done? Or perhaps volunteer to rebuild some dude's WP blog that got wiped out (wasn't properly backed up), from the remnants I found on archive.org? (6 hrs till tax deadline)
That's an impressive WTF display right there.
ReplyDeleteMaybe those lines really are traces of some state machine? Not that I would ever think of using such code, but it might had been autogenerated long ago and nobody wanted to touch it.
ReplyDeleteReminds me of "if all you have is a hammer, everything looks like a nail".
ReplyDeleteI would love to have a job where I could post code snippets, but the PTB disapprove of the whole idea.
ReplyDeleteI've had state machines similar to these, but - rarely (if ever) with just a simple inc. Normally, there would be some sort of logic at each case, deciding which case that would be the next.
ReplyDeleteThe only conceivable "benefit" I can think of, is that you would get away with one breakpoint, instead of one for each of the steps.
ReplyDeleteLars Fosdal Yes, were it not for the single "inc(i)" this code is a typical state machine implementation. It would branch depending on the state (represented by "i") until the exit condition is met. But in this case at least some of the called subroutines shoud have access to i and should be able to change it depending on their internal logic.
ReplyDeleteAlexander Elagin and i should be called "NextState" or something. I've written those, but I still like the C-style hack of making NextState a function pointer. It's worth a couple of clock cycles on most systems :)
ReplyDelete