- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
switch(&(bool*)42-0b10110)) { case 1: return nullptr; }
Admin
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.Admin
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.Admin
how much time it takes to find a syntax error?
Admin
constant expression required
Admin
30 years ago there was already a sensible alternative to switch(true):
https://reference.wolfram.com/language/ref/Which.html
Admin
Admin
And looong before that, you had the COND in lisp...
Admin
Terrible language and terrible programmer joined in an awful synergy, which produced this.
Admin
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
Admin
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.
Admin
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.
Admin
EVALUATE TRUE is a perfect way to implement a decision matrix in COBOL. However, you should know when to use IF.
Admin
Hey, I got an idea! F# has pattern matching. Look, mom, I can do that in php too.
Admin
The insidious thing is it's not a syntax error; it's declaring an unreferenced label called "casedefault".
Admin
The real WTF is using PHP or no mention to ternary operator?
Admin
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;
Admin
"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. :)
Admin
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.Admin
... 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.)
Admin
GRUMBLE, GRUMBLE ...
Associative arrays
?
Admin
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.
Admin
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.
Admin
Yeah, the site sensationalizes a bit. It's okay, though. That's why we're here.
Admin
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.
Admin
Makes no sense. A series of if statements communicates your intent more clearly.
Admin
Well said.
Admin
That's because Go is also a terrible language which needs to die.
Admin
(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.
Admin
Wow! I got the markdown right but typoed "I recall eons ago ..."
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
How is that? This is much more compact and clear:
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.
Admin
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");
Admin
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.
Admin
Right? I was quite surprised to see people arguing for the switch true anti pattern. Like, what??
Admin
Which once again proves how shitty PHP is, since you still can declare labels in a switch statement. But for what!?
Admin
I guess you haven't seen VB then? Because VB was the first place I ever saw a similar switch statement.
Admin
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.
Admin
Jumps within a switch statement, obviously.
Admin
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
Admin
Nope. He said the C
switch
is a programming atrocity.Admin
TRWTF is default fallthrough.