• (cs)
    switch( $_POST["operation"] == "editUser" ? "scEond" : "friSt" ) {
          case "scEond":// editUser
            // SNIP
            break;
          case "friSt"://saveUser
            // SNIP
            break;
    
  • Arkady (unregistered)

    My C++ compiler throws a warning if a switch statement has no default case, even if the default case is impossible to hit (e.g. the input is an enum and every possible case is covered.)

    Maybe the coder wants their code to run without warnings?

  • ab (unregistered)

    OMG this is more common than I expected! I had recently a member of my team who coded all IFs this way:

    if ($a==$b ? true :false) { // do something }

    At least he rectified them all when I told why that was so wrong.

  • ab (unregistered) in reply to Arkady

    The "default" clause is a WTF in it's own, but switch condition WTF is WTF'ier.

    He is running an IF clause with the "?" operand. So if $_POST["operation"] == "editUser" , it creates a 1, if not, a 2... it would be much more wiser to:

    if ($_POST["operation"] == "editUser") { // editUser } else { // saveUser }

    Also, this is PHP so there are no compiler warnings.

  • (cs) in reply to ab

    A former colleague was fond of if($boolean == TRUE) ...

  • Kuli (unregistered)

    The default case is necessary if the ternary operator returns FileNotFound.

  • (cs)

    What if one of the operands in the iif is null?

  • Pero perić (unregistered) in reply to ab

    It's not wrong, it's redundant.

    That's redundant, not wrong.

  • (cs) in reply to Arkady
    Arkady:
    My C++ compiler throws a warning if a switch statement has no default case, even if the default case is impossible to hit (e.g. the input is an enum and every possible case is covered.)
    Ever heard of ECC, bit errors, gamma rays, bad RAM, bugs in the CPU, and so on?
  • faoileag (unregistered)

    Just to give a possible explanation: the switch statement could well be left over from the "good old days"(tm) when the "operation" query param would carry integers instead of strings (operation == 1: edit user, operation == 2: save user etc).

    And for some reason, a developer found it easier to add an on-the-spot conversion of the string values into their (deprecated) integer versions, because it meant changing only one line of code instead of lots (gut's feeling is telling me, "// SNIP" stands for a lot of lines and not some single method call).

    And in certain environments, this can actually make sense: calling diff on the source files shows only the relevant change, not the noise generated by replacing the switch with the more natural if ... else.

    So yes, as it stands it is a WTF, but not necessarily one that should be changed.

  • Federico (unregistered) in reply to markfiend
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    A colleague of mine used the same "logic", until I asked him: "Nice, but why do you arbitrarily stop at the first level? Why not if((boolean==true)==true)? Or even if(((boolean==true)==true)==true)?

    He got enlightened!

    CAPTCHA: erat: how long since my last homework on Latin verbs!

  • Swedish tard (unregistered) in reply to faoileag
    faoileag:
    Just to give a possible explanation: the switch statement could well be left over from the "good old days"(tm) when the "operation" query param would carry integers instead of strings (operation == 1: edit user, operation == 2: save user etc).

    And for some reason, a developer found it easier to add an on-the-spot conversion of the string values into their (deprecated) integer versions, because it meant changing only one line of code instead of lots (gut's feeling is telling me, "// SNIP" stands for a lot of lines and not some single method call).

    And in certain environments, this can actually make sense: calling diff on the source files shows only the relevant change, not the noise generated by replacing the switch with the more natural if ... else.

    So yes, as it stands it is a WTF, but not necessarily one that should be changed.

    That's one of the biggest wtfs on behalf of programmers, and the one that pretty much causes most of the stress and headaches of maintainers.

  • David (unregistered) in reply to markfiend
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    Not really a WTF, it adds clarity, especially in a weak-typed language.

  • faoileag (unregistered) in reply to Swedish tard
    Swedish tard:
    That's one of the biggest wtfs on behalf of programmers, and the one that pretty much causes most of the stress and headaches of maintainers.
    Depends on the shop you work in - I have been lucky so far (*knock on wood*) but I seem to recall an article on the DWTF where someone was not capable of eliminating whitespace changes from their diff list and thus a policy was created that forbid changing indentation... in such a shop, an on-the-spot conversion will definitely be your friend, because otherwise you might get a headwash for unneccassary code changes.

    It still amazes me sometimes how little value some managers see in a maintainable code base...

  • fanalin (unregistered) in reply to David
    David:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    Not really a WTF, it adds clarity, especially in a weak-typed language.

    But then a type-safe operator should be used. if ($boolean === TRUE) {...}

  • Anonymous (unregistered) in reply to David

    In the case of php, sometimes I do that, yes, but that way: if ($boolean === TRUE)

  • clueless (unregistered) in reply to Federico

    I usually type if $boolean == TRUE in my code. I use it dependent on whether the code reads like a statement - if the variable name reads a proper statement - then I dont use it. Other wise for better readability I use it.

    so if the variable name is userIsAvailable - then I write the code as if userIsAvailable: do x

    If the variable name is availabilityOfUser - then I write the code as if availabilityOfUser == True do x

    or I might even create enums to make it more readable.

    The point is - shouldnt we be writing code to make it more readable than care about such arbritary coding standards - especially since in many cases compiler itself might remove it - or even otherwise what performance impact does it have?

  • faoileag (unregistered) in reply to Federico
    Federico:
    "Nice, but why do you arbitrarily stop at the first level? Why not if((boolean==true)==true)? Or even if(((boolean==true)==true)==true)?
    What, you didn't show him the merits of writing a specialized method "isTrue(boolean)"?

    And to be really on the save side, obviously isTrue's implementation must go all the way down: bool isTrue(bool aBool) { return isTrue(aBool); }

    Adding file_not_found to the solution can then be assigned as homework ;-)

  • Anonymous Paranoiac (unregistered) in reply to Anonymous
    Anonymous:
    In the case of php, sometimes I do that, yes, but that way: if ($boolean === TRUE)

    In PHP I do this by convention as it is a necessary defense mechanism against the WTF that is "truthy" and "falsy" comparisons.

  • mindwhip (unregistered)

    I've seen a lot of this recently

    if var1=false or var2=false or ... var30=false then return false else return true endif

  • MindChild (unregistered) in reply to markfiend
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

  • (cs)

    Is it me or we have a new design?

  • Arkady (unregistered) in reply to flop

    Okay - "...even if the default case is impossible to hit under normal/ideal conditions ..."

    Happy now? :-P

  • RobR (unregistered) in reply to flop

    Richardson's First Law of Computational Unpredictability states: "The fact that a given occurrence is impossible does not imply that it will not occur."

  • MiniMax (unregistered)

    A snippet from a piece of Java code in "my" system:

    Boolean lbBusinessEbox = Boolean.TRUE; ... if (lxUserInfo.getBusinessFlag().equals(Boolean.FALSE)) lbBusinessEbox = Boolean.FALSE;

    It took me several passes and a quick consulting by a friend to make I sure I really understood what was going on.

  • Almafuerte (unregistered) in reply to Arkady

    Maybe the coder should have used an IF statement instead of wrapping a ternary operation inside a switch statement.

  • EuroGuy (unregistered) in reply to flop
    flop:
    Ever heard of ECC, bit errors, gamma rays, bad RAM, bugs in the CPU, and so on?
    If every conditional statement in your code covers for the possibility of bad RAM, I don't want to see your code - ever.
  • Boomer (unregistered)

    and I feel fine...

  • Anonymous Paranoiac (unregistered) in reply to RobR
    RobR:
    Richardson's First Law of Computational Unpredictability states: "The fact that a given occurrence is impossible does not imply that it will not occur."

    The Second Law states: "If something is possible, it will happen. Especially if users are involved."

    I've had many conversations with account managers that run along these lines: Me: "What is the expected behavior in scenario XYZ where a user chooses options A and B?" (note: there is nothing at all preventing users from choosing both) Them: "But that will never happen!" Me: "..."

  • QJo (unregistered)

    Not a WTF, merely an idiosyncrasy based upon a rigorously enforced house style. I'd have absolutely no problem if I were to see this in code maintained by my staff; it doesn't even require a comment, beyond an indulgent chuckle at what cards they all are.

  • Dave (unregistered) in reply to MindChild
    MindChild:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

    So you're saying that you're basically Hard of Understanding?

  • Kasper (unregistered) in reply to EuroGuy
    EuroGuy:
    If every conditional statement in your code covers for the possibility of bad RAM, I don't want to see your code - ever.
    I would however like to see the look on his face, when he realizes what he forgot to check for. He did not think about the possibility of one of the bits in the PC (program counter) register getting flipped right as the CPU entered one of the branches.
  • (cs) in reply to MindChild
    MindChild:
    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

    In that case, why don't you name your Boolean variable to something useful, like $userIsAdmin or $TransactionCompleted? Then the code reads:

    "if $userIsAdmin"

    and it makes sense.

  • Swedish tard (unregistered) in reply to MindChild
    MindChild:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

    Adding cruft because of naming variables properly does not make adding cruft any better...

  • Swedish tard (unregistered) in reply to Swedish tard
    Swedish tard:
    MindChild:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

    Adding cruft because of naming variables properly does not make adding cruft any better...

    ** Because of failing to name... Brain go poopoo...

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to mindwhip
    mindwhip:
    I've seen a lot of this recently

    if var1=false or var2=false or ... var30=false then return false else return true endif

    There are two kinds of people. Those who understand Boolean logic, and those who don't, and those who don't, and those who don't, and those who don't, and those who don't, and those who don't, and those who don't, and...
  • Dan (unregistered) in reply to clueless
    clueless:
    so if the variable name is userIsAvailable - then I write the code as if userIsAvailable: do x

    If the variable name is availabilityOfUser - then I write the code as if availabilityOfUser == True do x

    If you are really interested in the readability of the code then rename the damn variable.

  • Dan (unregistered) in reply to David
    David:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    Not really a WTF, it adds clarity, especially in a weak-typed language.

    The real WTF is weak-typed languages.

    CAPTCHA: inhibeo: the spell that causes you to not get laid

  • (cs)

    No joke about president's daughter yet.

    BTW- margaret thacher is dead. please mourn her for 1 minute.

  • Raph (unregistered) in reply to MindChild

    If your $boolean has a name that actually means something - e.g. $nukeReady, or $reticulatingSplines, then the if statement reads naturally:

    if ($nukeReady) --> if the nuke is ready, then...

    if (!$reticulatingSplines) --> if not reticulating splines, then...

  • Raph (unregistered) in reply to MindChild

    (comment fix FTW)

    MindChild:
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?

    If your $boolean has a name that actually means something - e.g. $nukeReady, or $reticulatingSplines, then the if statement reads naturally:

    if ($nukeReady) --> if the nuke is ready, then...

    if (!$reticulatingSplines) --> if not reticulating splines, then...

  • just me (unregistered) in reply to markfiend
    markfiend:
    A former colleague was fond of if($boolean == TRUE) ...

    Very useful pattern in C, in conjunction with:

    #define TRUE 0 #define FALSE 1

  • faoileag (unregistered) in reply to EuroGuy
    EuroGuy:
    flop:
    Ever heard of ECC, bit errors, gamma rays, bad RAM, bugs in the CPU, and so on?
    If every conditional statement in your code covers for the possibility of bad RAM, I don't want to see your code - ever.
    Not every. You only need to do this with switch-statements. And yes, with switch-statements, a default branch is a sensible thing to have. Some coding styles even mandate them :-)
  • (cs) in reply to Almafuerte
    Almafuerte:
    Maybe the coder should have used an IF statement instead of wrapping a ternary operation inside a switch statement.
    Thank you, Captain Obvious.
  • (cs)

    It's still the best PHP I've ever seen, WTFs and all.

  • Valued Service (unregistered) in reply to clueless
    clueless:
    I usually type if $boolean == TRUE in my code. I use it dependent on whether the code reads like a statement - if the variable name reads a proper statement - then I dont use it. Other wise for better readability I use it.

    so if the variable name is userIsAvailable - then I write the code as if userIsAvailable: do x

    If the variable name is availabilityOfUser - then I write the code as if availabilityOfUser == True do x

    or I might even create enums to make it more readable.

    The point is - shouldnt we be writing code to make it more readable than care about such arbritary coding standards - especially since in many cases compiler itself might remove it - or even otherwise what performance impact does it have?

    I solve that easy.

    I only name a variable "PropertyOfX" if it is not a boolean. This means a comparison is necessary. It flags me to look for the enum values, or such. If it is a boolean I always use the "IsPropertyValue". If it is a nullable I put "nullable" in the name.

    IsUserAvailable is a boolean.

    IsUserAvailableNullable is a nullable boolean.

    UserAvailability is an enum. (AvailableWeekdays, AvailableWeekends, AvailableAfterHours).

    UserAvailabilityFlags is an enum with flag attribute set.

    It's what hungarian notation should have been.

  • Just a name (unregistered) in reply to Dave

    Well, knowing that $boolean does not eally needs to be holding a boolean (the infamous 'True, false or File_not_found' joke comes to mind) I guess it would not bea too bad an idea to explicitily name it. But than, as other posters here already remarked, when using PHP rather with the type-safe comparision ("===").

    Especially if many people will be editing/adding to that code ...

    ... or ofcourse use a strong-typed language, and raising a red flag when a non-boolean is used where one is expected

  • Valued Service (unregistered) in reply to mindwhip
    mindwhip:
    I've seen a lot of this recently

    if var1=false or var2=false or ... var30=false then return false else return true endif

    So they wiped out the real values all with false, then always return false.

    TRWTF is languages without a distinction between value and comparison operations.

  • Anonymous Paranoiac (unregistered)

    Two of my favorite PHP WTFs involving bolean logic:

    switch ( $variableThatHappensToBeTruthy ) {
    	case (expression involving $foo and $bar):
    		/* snip */
    
    		break;
    	case (expression involving $foo and $bar):
    		/* snip */
    
    		break;	
    	/* snip */
    	default:
    }
    
    which is equivilent to:
    switch ( true ) {
    	case (expression involving $foo and $bar):
    		/* snip */
    
    		break;
    	case (expression involving $foo and $bar):
    		/* snip */
    
    		break;	
    	/* snip */
    	default:
    }
    
    While I appreciate what the coder was trying to do and it would be helpful at times to have a switch structure that supported expressions, it's still a WTF.

    The second is this lovely expression (I really hate negative 'or' comparisons):

    if(!(($booleanOne === false)||($booleanTwo === false)))
    
    which is equivilent to:
    if ( $booleanOne !== false && $booleanTwo !== false )
    
    For those of you about to call WTF, the expression can't be converted to:
    if ( $booleanOne === true && $booleanTwo === true )
    
    Because the two values are results of PHP functions which either return false on failure or "something else" on success.
  • Valued Service (unregistered) in reply to Just a name
    Just a name:
    Well, knowing that $boolean does not eally needs to be holding a boolean (the infamous 'True, false or File_not_found' joke comes to mind) I guess it would not bea too bad an idea to explicitily name it. But than, as other posters here already remarked, when using PHP rather with the type-safe comparision ("===").

    Especially if many people will be editing/adding to that code ...

    ... or ofcourse use a strong-typed language, and raising a red flag when a non-boolean is used where one is expected

    Then, when would you ever not use "==="? When you need to know if "Dog" is equivalent to 23?

Leave a comment on “Switching It Up at the End of the World”

Log In or post as a guest

Replying to comment #:

« Return to Article