• NULLPTR (unregistered)

    switch(&(bool*)42-0b10110)) { case 1: return nullptr; }

  • VI (unregistered)

    switch true is kinda recommended/encouraged in Go, and has a short syntax: https://tour.golang.org/flowcontrol/11 I feel that in the (uncommon) case of having a longish if-elseif-else chain, this is actually nicer.

    But not being PHP, the compiler will error out on the casedefault thing.

  • VI (unregistered)

    switch true is kinda recommended/encouraged in Go, and has a short syntax: https://tour.golang.org/flowcontrol/11 I feel that in the (uncommon) case of having a longish if-elseif-else chain, this is actually nicer.

    But not being PHP, the compiler will error out on the casedefault thing.

  • not a robot (unregistered)

    how much time it takes to find a syntax error?

  • bvs23bkv33 (unregistered)

    constant expression required

  • P. Wolff (unregistered)

    30 years ago there was already a sensible alternative to switch(true):

    https://reference.wolfram.com/language/ref/Which.html

    Which[test_1, value:1, test_2, value2, …] evaluates each of the test_i in turn, returning the value of the valuei corresponding to the first one that yields True.

  • P. Wolff (unregistered) in reply to bvs23bkv33

    constant expression required Not in every programming language.

  • NewtonIsaacs (unregistered) in reply to P. Wolff

    And looong before that, you had the COND in lisp...

  • (nodebb)

    Terrible language and terrible programmer joined in an awful synergy, which produced this.

  • P (unregistered)

    switch(true) is almost definitely written by clueless developers who, upon hearing the phrase "if you want to test out a single value against multiple values, use switch-case instead of if-chains, it's MORE PERFORMANT that way", holds the dogma to always use them FOR PERFORMANCE.

    The issue is that switch-case only works when your case values are unique, static expressions. That's the hidden, unspoken assumption of them, as otherwise it cannot be turned into a constant time branch when compiled down. Some language like C# outright reject non-static case expressions as a compiler error; of course, it doesn't stop people from wanting to use them because their wrong belief would make them feel bad unless they put one into their code. Hence the switch(true) pattern.

    In a way they're just like some client requirements: utterly stupid, and coming from wrong conceptions about some of the things that should've been common sense...

    Oh, and by the way, even Jeff Atwood has said that switch statements is a "programming atrocity": https://twitter.com/codinghorror/status/909900250

  • Kattman (unregistered)

    For once, while PHP is used here, PHP is not the WTF, this is purely a dev wtf. Those are the best (worst) kind to have.

  • Scott (unregistered)

    This is the sort of thing that makes me wonder, "how could that even occur to someone"?

    We had a cow-worker build a process to mask data so we could do restores to non-production systems (yeah, even that idea is a WTF, but there we are).

    So, this guy builds a table which has the tables and columns to be masked and the mask to apply. Good start, so far.

    The process is: restore production to a local db. Run a console app which reads the above table and then: 1) creates a copy of the table 2) inserts into copy table, applying the mask 3) drops all FKs for real table 4) drops real table 5) renames copy to real table name

    When asked why this is not just generating update statements on the table directly, the answer was: "well, this runs in only a minute."

    I DGAF how fast or slow it is. It's an offline process that we can wait for. It the not understanding basic development that concerns me.

    Needless to say, this person is an HPC, and everything he writes shows this same level of cluelessness.

  • Old White Man (unregistered)

    EVALUATE TRUE is a perfect way to implement a decision matrix in COBOL. However, you should know when to use IF.

  • (nodebb)

    Hey, I got an idea! F# has pattern matching. Look, mom, I can do that in php too.

  • Simon Clarkstone (unregistered) in reply to not a robot

    The insidious thing is it's not a syntax error; it's declaring an unreferenced label called "casedefault".

  • Anon (unregistered)

    The real WTF is using PHP or no mention to ternary operator?

  • LegacyWarrior (unregistered)

    Now, if all you're doing is assigning a single value, this sounds like the perfect opportunity for a simple (yet much maligned) nested ternary. (and pick your formatting and alignment of choice) $result = ($a&&$b) ? 'b' : (!$a&&!$b) ? 'c' : $a ? 'a' : 'unknown'; return $result;

  • Kasha (unregistered)

    "And that’s the real WTF in this case. The developer responsible for the code produces a lot of this kind of code. They never use an if if they can compact it into a switch. They favor while(true) with breaks over sensible while loops."

    I actually never said that. For the most part the developer makes some pretty solid code. Everyone has a bad day now and then. :)

  • (nodebb)

    I think this varies by language. In ye olde VB, I really liked select case true. Evaluate conditions in order, return on the first matching condition. It was perfectly logical/readable.

  • Sole Purpose of VIsit (unregistered) in reply to Simon Clarkstone

    ... and the same syntax wart is permissible in C, as well, so I can't even blame PHP. (Which is annoying.)

    Funny that. Fifteen or so years of programming in C, and I've never come across this particular horror. Presumably both languages allow you to jump into a switch statement from the outside, which is arguably even more of a WTF than approximating a pattern-match in PHP.

    (And if anybody wants pattern matching in PHP, try googling for those two terms plus github. Me, I wanted to gouge my eyes out, but YMMV.)

  • steve76 (unregistered)

    GRUMBLE, GRUMBLE ...

    Associative arrays

    ?

  • MiserableOldGit (unregistered)

    I thought that wasn't that bad, leaving aside the typo, it just needs a comment in the cases to explain to the unfamiliar what the logic really is as it's not hard to misunderstand at first glance.

    I may be polluted as I spent a while (geddit?) working in a software house where while/switch loops were considered best practice, with the condition for both the loops and the switch being set inside cases and liberal goto statements redirecting the flow. They were migrating to VB, so these became Do/Select loops and no-one could cope with the difference in behaviour from switch to select so they whinged like hell about VB and added a another sprinkling of goto statements and honky conditional variables jumping the program flow all over the place. Why is it mediocre programmers always think they must be cleverer than the language when things start looking complicated?

    That codebase would have had Mother Theresa drop-kicking orphaned infants into an active volcano within a week. Maybe two.

  • tbo (unregistered) in reply to P

    Whelp, he's wrong. switch / case is just syntactic sugar that makes code more readable. There's nothing inherently superior about if/else if/else blocks, especially in a language like Swift, where breaks are implicit.

  • tbo (unregistered) in reply to Kasha

    Yeah, the site sensationalizes a bit. It's okay, though. That's why we're here.

  • (nodebb) in reply to tbo

    So much wrong is said here, I cannot believe it. Ok switch is not just a syntactic sugar. It imposes constraints on the condition which allows compilers to optimize it. Swift is a terrible language in almost every way and needs to die. Breaks being implicit or explicit is irrelevant.

  • (nodebb) in reply to bradwood

    Makes no sense. A series of if statements communicates your intent more clearly.

  • (nodebb) in reply to P

    Well said.

  • (nodebb) in reply to VI

    That's because Go is also a terrible language which needs to die.

  • WTFguy (unregistered)

    (Wish me luck on my blind no-edit use of WTF-ey MarkDown) ;) ...

    I recall ons ago using the switch(true) ... idiom in a language that had
    if (boolExpression) SomeSingleStatement [/else SomeOtherSingleStatement ]
    but did not support a multi-statement block on either the if or the else. It also did not provide an elseif chaining structure. Each if construct had at most 2 alternatives. Not pretty.

    For whatever benighted reason the language also supported
    switch(true)
    followed by a series of n different
    case(boolExpression) SomeStatementBlock .

    That was a huge improvement in every way. Once you got used to how to read it.

  • WTFguy (unregistered)

    Wow! I got the markdown right but typoed "I recall eons ago ..."

  • tbo (unregistered) in reply to Mr. TA

    Oh, you're one of those people, aren't you? No language is as good as your language. Maybe you've forgotten that not all languages are compiled. This is one of them.

  • AleD (unregistered) in reply to Kattman

    Really? Because that switch true syntax with expressions in the 'case:'s and not catching the 'casedefault' error seems from an awful and unsafe language to me. It may be that I never programmed in PHP but this doesn't look good.

  • Boris Karticz (unregistered)

    The moment I saw "switch(true)" I thought "PHP", and guessed correctly. That's the only language I've seen it in, given that the majority of other languages with switch() only allow constants in the cases. Not really a WTF, just an idiomatic way of pattern-matching. if/else chains are bloated syntactic sugar anyway.

    Funny to see a comment about ternary, PHP is I think the only language that has messed up its precedence.

  • (nodebb) in reply to Sole Purpose of VIsit

    Rest assured, PHP does not allow you to do that (use goto to jump into a loop/switch statement). C, on the other hand, does.

  • David (unregistered)

    It’s more compact than a chain of ifs.

    How is that? This is much more compact and clear:

    if ($a && $b) return 'b';
    if (!$a && !$b) return 'c';
    if ($a) return 'a';
    return 'unknown';
    

    Actually the breaks in the original are completely redundant, when you have return statements they are never reached. But even if you remove them, the above is still shorter.

  • Your Name (unregistered)

    I don't see the point of 'break'ing after a 'return' statement, except for encouraging the compiler to emit spurious warnings ("Unreachable code encountered");

  • Your Name (unregistered) in reply to David

    Actually the breaks in the original are completely redundant

    I wouldn't call that redundant. You make the compiler complain about unreachable code whereas the compiler does not complain if you leave them out. So those breaks do have an effect.

  • (nodebb) in reply to David

    Right? I was quite surprised to see people arguing for the switch true anti pattern. Like, what??

  • David (unregistered) in reply to Watson

    Rest assured, PHP does not allow you to do that (use goto to jump into a loop/switch statement). C, on the other hand, does.

    Which once again proves how shitty PHP is, since you still can declare labels in a switch statement. But for what!?

  • Ray (unregistered) in reply to Boris Karticz

    I guess you haven't seen VB then? Because VB was the first place I ever saw a similar switch statement.

  • (nodebb)

    I've used DECODE(True, cond1, result1, ..., condn, resultn, default) in Informatica (a data processing tool). When you have a whole bunch of conditions to check in a single expression, a syntax like this with one condition and one result expression on each line and a single closing bracket at the end is clearer than a sequence of nested IF(...) expressions with other expressions (including other IFs) in the result clauses.

    I have to admit I originally started using DECODE(True, ...) in SQL, but these days I prefer to use CASE WHEN cond1 THEN result1 ... WHEN condn THEN resultn ELSE default END. It's wordier, but clearer.

    The one thing you don't want to do with a DECODE(True, ...) statement is misplace a comma, particularly if you do so in such a way as to not cause a syntax error. If you do, your conditions and result expressions after the error switch roles, which is not likely to give you the results you expect.

  • (nodebb) in reply to David

    Jumps within a switch statement, obviously.

  • Meet (unregistered)

    What is wrong in writing while (true) ? I come from SQL background and always write while (1=1) whenever I need to do row processing.

    I think it is more of personal choice. Yet, from this article, it seems like while(True) is actually not a good practice anywhere

  • (nodebb) in reply to P

    Oh, and by the way, even Jeff Atwood has said that switch statements is a "programming atrocity"

    Nope. He said the C switch is a programming atrocity.

  • Future (unregistered)

    TRWTF is default fallthrough.

Leave a comment on “Switch Off”

Log In or post as a guest

Replying to comment #:

« Return to Article