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.

Comments

  1. Eric Grange I have no idea.  None -- totally bizarre.  That kind of thing is in more than one place.

    ReplyDelete
  2. maybe to be sure that you'd never multithread that code...

    ReplyDelete
  3. Are DoSomething/DoMore/DoEvenMore local procedures that modify the value of i?

    ReplyDelete
  4. Martin Wienold Nope.  It's as bad as you think.

    ReplyDelete
  5. Seriously, 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.

    ReplyDelete
  6. may be some time ago  it looked like
    while i < 40...
      case (i mod 3) of ?

    ReplyDelete
  7. 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.

    ReplyDelete
  8. I think the procrastination module in my brain contains code like that.

    ReplyDelete
  9. Simon 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.

    ReplyDelete
  10. Simon Stuart So was I...and truthful at the same time.:-P

    Now, 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)

    ReplyDelete
  11. That's an impressive WTF display right there.

    ReplyDelete
  12. Maybe 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.

    ReplyDelete
  13. Reminds me of  "if all you have is a hammer, everything looks like a nail".

    ReplyDelete
  14. I would love to have a job where I could post code snippets, but the PTB disapprove of the whole idea.

    ReplyDelete
  15. I'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.

    ReplyDelete
  16. The only conceivable "benefit" I can think of, is that you would get away with one breakpoint, instead of one for each of the steps.

    ReplyDelete
  17. Lars 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.

    ReplyDelete
  18. Alexander 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

Post a Comment