• NotAThingThatHappens (unregistered)

    If the current tally is -0 and you add +0 what is the result?

    I guess it is frist!

  • (nodebb)

    It looks to me like the values are Left, Right, Both, and Neither, and this code is just counting whatever the things are. So if it's Neither, skip as there's nothing to add. Otherwise add the number of things to the tally - Left and Right are one thing, Both is two things, N is zero things, and if it's anything else that counts as zero as well. Apart from the redundancy in the code, that's hardly an ideal way of encoding the data - two Booleans for Left and Right would be much simpler.

  • RLB (unregistered)

    code that evolves is bad code- code should change over time, as the environment changes, but it should change in a structured, methodical, and well understood process, not through evolution.

    I agree in principle. Unfortunately, all too often the demands on the code evolve, and not structuredly or methodically; and the code has to evolve with them. (Can you tell I've had to deal with customers who don't know what they want and once you have interviewed them enough to nail their wants to the paper, change their mind two months into development? To be effective one month ago?)

  • TS (unregistered)

    There's good Hungarian notation [citation needed], bad Hungarian notation, and bad Hungarian notation done badly. "rsRecordSet"?

  • Sauron (unregistered)

    Quick, delete this article! The world must not know about that crappy if-switch anti-pattern.

    Otherwise the re-incarnation of Edgar Dijkstra will write an article titled "SWITCH STATEMENT CONSIDERED HARMFUL" and most languages will remove support for switch statements just like they remove support for goto.

  • Scott (unregistered)

    One could argue that it's intended to make the code self documenting by including case elements for all possible values, with the "case else" there as a belt-and-suspenders defense against unknown values. That may be giving it unwarranted credit though since there are much better ways to document than using that "+0" nonsense. I have to wonder if any of that is optimized-out by the compiler?

  • MJ (unregistered)

    Just one thought in the initial 'N' check: This is something that can improve code speed if you have a time-consuming test that doesn't apply most of the time. If 100,000 records are N, and only 1,000 have something to consider, then that initial check speeds up most operations. Now, the switch should be good enough in this particular case, but the principle is good. It has more than doubled speed of processing for me in the past. As for the other aspects, WTF?

  • Duston (unregistered)

    "rsRecordSet"? IMHO, it could have been worse, they could have named it "rsTheRecordSet"

  • (author) in reply to Scott

    As this is VB6, I doubt the compiler was terribly smart about optimizations at all.

  • (nodebb)

    If this article is not a WTF, but still it's a WTF, then don't not ignore it.

  • Sole Purpose of Visit (unregistered) in reply to Sauron

    Leaving aside FP, the only two languages I can think of that do not support goto are Java and Python. (Weirdly, PHP introduced goto in v5.3 or thereabouts.)

    Not many languages outside C support longjmp (a goto that ends up outside the current stack frame), but that's probably all to the good. It's just a poor man's version of exceptions.

    In fact, you could reasonably emulate goto in Java and Python (and presumably others, again leaving FP to one side) via exceptions....

    Not that I'd recommend it in this case.

  • Yazeran (unregistered) in reply to Scott
    <quote> That may be giving it unwarranted credit though since there are much better ways to document than using that "+0" nonsense. </quote> Well One evil reason for this could be if they overloaded the + operator (assuming that is even possible in the language used) so it had side effects (and in that case we would be facing a WMD level WTF, but you never know...)

    Yazeran

  • (nodebb) in reply to Sole Purpose of Visit

    Leaving aside FP, the only two languages I can think of that do not support goto are Java and Python.

    Also Modula-2.

  • BobG (unregistered)

    "rsRecordSetD1T3" might be in the running for the worst identifier I've ever seen.

    It's not just the Hungarian notation - it's the redundant expansion of the Hungarian notation that makes the Hungarian notation even MORE unnecessary.

    And then the icing on the gilded lily: the only non-boilerplate part of the identifier, D1T3, is utterly cryptic.

  • Chris (unregistered) in reply to BobG

    D1T3, is utterly cryptic

    We'll just need to read up on the mandatory company naming guidelines, I guess. Database 1 Table 3. It's so easy and well structured!

  • Fizzlecist (unregistered) in reply to Steve_The_Cynic

    It's been a while so I could be wrong but I don't recall a GOTO in LISP, Scheme, PROLOG , ML or Haskell. Many of the languages that do still have it have limited it's usefulness

  • (nodebb) in reply to BobG

    Oh, I've seen a worse one. A function called 'IsTicketnnnnn' (where nnnnn was the ticket number this function was written to support). I complained at code review that this was an entirely unhelpful function name, as it didn't describe what it was doing and none of the parameters was actually a ticket, but I was overridden because they couldn't think of a better one and the project was due for release.

  • Jan (unregistered) in reply to thosrtanner
    Comment held for moderation.
  • (nodebb) in reply to BobG

    It's not cryptic at all, the function records whether a relay block is set or not and that's located next to diode 1, transistor 3 on the Rev.19 PCB. Perfectly simple and logical really.

  • (nodebb) in reply to Scott

    One could argue that it's intended to make the code self documenting by including case elements for all possible values, with the "case else" there as a belt-and-suspenders defense against unknown values. That may be giving it unwarranted credit though since there are much better ways to document than using that "+0" nonsense. I have to wonder if any of that is optimized-out by the compiler?

    I don't find the +0 to be bad. It makes it very clear that you do nothing, I would expect the compiler to remove it. In a situation like this I would prefer the N branch have the +0, the only real WTF I see is testing for the N case rather than simply using the case to make it a nop.

  • Randal L. Schwartz (google)

    Improved by GPT3 with "improve this" being the only prompt:

    If rsRecordSetD1T3("ItemState") <> "N" then tally = 0

    Switch rsRecordSetD1T3("ItemState")
    Case "R", "L"
        tally = tally + 1
    Case "B"
        tally = tally + 2
    Case Else
        tally = tally + 0
    End Switch
    …
    

    End If

  • Prime Mover (unregistered)

    There's no break in the case statements, so out of this routine you'll get either 2 or 3, not the 1 or 2 tha you would at a glance expect.

  • Brian Boorman (unregistered) in reply to Prime Mover

    Since this is VB6 there aren't break statements. Cases don't fall through like C.

  • Argle (unregistered)

    In the "happily ever after" portion of this, using the whole awful mess with loops and scattered comparisons of all kinds (including some dead code) I managed to get the whole thing as a single SQL statement. Well, a single SQL operation that was about 180 lines long.

    Also, SteelCamel2 is correct: Left, Right, Both and Neither stored as a code in the database. There was one additional WTF in the data. There was "markup" field in the database that had both a $ markup and a % markup. Yeah, both in the same field. It was % if below 2 and $ otherwise. So much fun.

  • سمارت بتس (unregistered)
    Comment held for moderation.
  • Ali (unregistered)
    Comment held for moderation.
  • Craig (unregistered) in reply to Remy Porter
    Comment held for moderation.
  • (nodebb) in reply to thosrtanner

    I reckon writing software is 1/3 working out how to solve the problem; 1/3 banging at the keyboard; and 1/3 trying to come up with decent names for things.

Leave a comment on “Counting on Switching”

Log In or post as a guest

Replying to comment #:

« Return to Article