
Switch on True (2008) - userbinator
http://www.jayjohnston.com/php/switch-case-statement-switch-on-true.html
======
Sharlin
No, please don't do this. It's "clever" in all the wrong ways. It's not
idiomatic use of switch, it's _more_ code and line noise than simple if/else
(all the switch(true), cases, and breaks are just syntactic salt), and it's
not nearly as intuitive what happens if your conditions don't happen to be
disjoint (in languages with a C-style switch you can control this to an extent
with breaks vs. fallthrough but needless to say it's _extremely_ fragile and
confusing).

Even in languages with proper pattern matching it makes no sense to do
something like the following instead of if/else:

    
    
      match x {
        x if x < 20 => foo(x),
        x if x >= 20 => bar(x),
        ...
      }
    

Avoiding complex if/else chains is a good idea, but replacing them with the
same thing, only spelled differently , is not the way to do it.

~~~
UweSchmidt
What is the way to do it?

I like having functions with expressive names to encapsulate/hide complexity,
and to comment what I'm doing. If it is not obvious why "20" is such a big
deal in your code, then that's where I would trip up.

I don't mind having a few ifs if they look like this:

    
    
      if (isHoliday()) { }
      else if (isWeekend()) { }

~~~
Sharlin
Sure. But _complex_ if/else logic, like I said, is often a code smell that
indicates that the behavior might be better off abstracted out to a
polymorphic entity of whatever sort is idiomatic in your language. There are a
lot of "If statement considered harmful" style articles on the web, but I
can't seem to find the specific one I had in mind :/

------
ema
I think I still prefer chaining "else if"s but I do appreciate seeing this new
perspective on something I thought I knew like the pockets of my pants.

------
jasode
Yes, as a positive, I see that _switch(true)_ will _cosmetically_ align the
tests of 'possible value 1' and 'possible value 2'.

But the negative is that you've added the requirement to always remember to
include _" break;"_ after each block. The code is prettier but also _more
fragile_. This type of vigilant programmer discipline required to keep the
code error-free (especially across multiple programmers) is not easy. Missing
"break" statements causing unintended fallthrough are common bugs discussed in
many "traps & pitfalls" books/blogs.

The "else if" while not as vertically aligned, includes the invisible "break;"
for free.

(Side note that some languages like C#'s and Rust's switch statement don't
have fallthrough behavior -- but this article uses PHP which does.)

------
Smaug123
It makes you long for a dependently typed language where you really and truly
_can_ switch on decidable relations like "compare".

For example, I have some Agda code (totally unreadable to anyone who didn't
write it, i.e. anyone who isn't me) where I switch on whether a+1 is less
than, equal to, or greater than x+1, as part of the inductive step in an
implementation of the division algorithm, rendering a number into a quotient
and a remainder on division by another number. The type system guarantees that
I haven't missed any cases.
([https://github.com/Smaug123/agdaproofs/blob/9e22ba78f5446ce7...](https://github.com/Smaug123/agdaproofs/blob/9e22ba78f5446ce7272ee342affdfafa2ae189ae/PrimeNumbers.agda#L76))

~~~
masklinn
TBF for cases which are not overly complex like your case you just need the
right enums and functions e.g. [https://play.rust-
lang.org/?version=stable&mode=debug&editio...](https://play.rust-
lang.org/?version=stable&mode=debug&edition=2018&gist=898a93af528fd3d164b166dbea0436e6)

------
Znafon
If you really wanted to align your clauses, you also could do:

    
    
         if (false){
         } else if ($var=='possible value 1') {
           echo $var;
         } else if ($var=='possible value 2') {
           error_log($var);
         } else {
           // do nothing
         } 
    

Now we just to find somewhere where it would be a good idead...

------
jchw
I’ve used this trick before. Thankfully in Go you would never need to do this,
because in Go a switch statement with no predicate behaves this way by
default.

------
gowld
Ah, a lovely throwback to the bad old days of PHP and PHP programmers.

This is an ugly port of Lisl's "cond".

------
lostmsu
For expression statements I usually chain .. ? .. : .., breaking lines before
:

------
heptathorp
Everything about this is very (2008)

1\. Skeuomorphic torn-paper edge at the bottom

2\. Loud background image on the body

3\. Text is unreadably small on mobile

4\. php

