29

You wanna know why PHP has such a poor reputation?
It's because of imbeciles who code like this:

Comments
  • 2
    Wait, what?
  • 9
    Ewwwwwwwwwwwwwww.

    Can we just erase people from the gene pool for psychopathically wrong code?
  • 4
    What is this... thing I'm looking at.
  • 10
    That’s an elegant piece of shit code
  • 2
    Our legacy code has too many of these "switches"
  • 5
    @ThatPerlDeb The biggest switch I've found in this "code base" is 3600 lines of code. One big switch with some smaller ones within... ⚰️⚰️⚰️
  • 3
    Straight to jail
  • 4
    oh wow, you can do that?
  • 2
    i....did that....😶
  • 2
    @sleek Whyyyyyyyyyyyyyyyyyyyyyyyyy?
  • 1
    How does anyone even come up with this idea.

    What happens if multiple cases are true?
  • 1
    @IntrusionCM sometimes you write a lot of consecutive if conditions, then u think to yourself, hmmm this looks too ugly, maybe theres a nice way to wrap that logic up.

    then i googled it and someone suggested it online and I tried it.

    Looking at it now it looks horrible, i feel bad, and im sorry
  • 1
    @sleek At least progress is made.

    Maybe as a hint: The easiest solution would be an associative array binding a callback.

    With newer PHP versions, lambdas are possible.

    For a more pimped functional style, look at e.g. functional-php:
    https://github.com/functional-php/...
  • 1
    it's identical to 2 if statements one after other, no improvement though
  • 3
    @theabbie the comment says "500 more lines".

    If elseif else bombers aren't good either.
  • 0
    @theabbie 2 ifs are not quite the same as an if-elseif. In the latter, only one block may ever be executed, just like in switch-case. But other than that, you're absolutely right, an if-elseif would have veen much cleaner.

    @ all:
    My assumption why they wrote this is that they wanted to show which variable was being checked for each case block...
    But then again, there are wayyyy cleaner ways of doing this lol
  • 1
    Well... this looks like several copy n paste stack overflow snippets combined into one piece of code.

    At least I think it will work but working code is not the only but just the first requirement of code. I assume that this wasn't written by a professional.
  • 0
    Switch case is ugly af in any language
  • 3
    @ChristoPy your mom is feio in any language
  • 0
    @electrineer afaik a string cannot be equal to two different strings at the same time.
    Also, the switch will go in order, so the first being equal will trigger and the break will end the switch.
  • 0
    @satibel what prevents you from using any other condition than string comparison if that is allowed. Interesting that anything is allowed.
  • 1
    @electrineer I just assumed it was copy pasted and all the same, but yeah there may be different comparisons within the switch. Still the first match will do its thing and then break the switch (assuming a break is used).
    If conditions like starts with or contains are used, and the first match priority is used, I'm not sure if there's a much cleaner way to do dispatch (a db might work but it'll be much slower).
    That's basically chained if/else except you can be sure there's not an end somewhere with a forgotten else or one purposefully put there (though there's the same-ish thing with break)
  • 1
    The best part about this snippet is that the Php interpreter will optimize the fuck out of it. It will not run any slower than "good, readable" code. The Php maintainers have become so good at optimizing shit code that most Pho
    P developers don't care about quality code anymore haha
  • 2
    @Lucky-Loek true but turd coders will still fetch redundant data from outside systems at every opportunity they can get. O(n²) for the win 😄
  • 0
    Dafuq
Add Comment