• Quite (unregistered)
    //i should have fristed this code
    return (frist == 1 ? 1 : frist)
    
  • Michael R (unregistered)

    function fristorno($in) { if($in == 'frist') return 'frist'; else return 'NO'; }

  • (nodebb)

    That PHP snippet is actually no that bad. It coerces the function input to one of two possible values - "YES" or "NO". In and of itself, there's nothing wrong with it. Now, it could be a hint that there's something really wrong somewhere else... but it could also be a part of a decent code that has to interface with some nasty data from a poorly designed system.

  • my name is missing (unregistered)

    I should have coded this stupid comment.

  • NotAThingThatHappens (unregistered)

    I stupidly coded this should have comment.

  • Allie C (unregistered)

    I don't know why, that ternary has me giggling like a mad woman

  • Robin (unregistered) in reply to Vilx

    I came here to say similar. There's certainly WTFs lurking behind this (I hate the stringly-typed booleans) but this particular function seems fine in that context.

  • (nodebb) in reply to Vilx

    The thing I dislike about it isn't that it coerces everything non-"YES" to "NO", or at least not as such.

    No, the problem is that it coerces "yes", "Yes", "YeS", "YEs", "yEs", "yES", and "yeS" to "NO", which is a bit POLA-breaking.

  • MaxiTB (unregistered)

    That's all classical typeless language nonsense. Especially PHP is has a crazy amount of method that will return a different type when an error occurs (like return a string on success or null otherwise FALSE). Now someone could argue that this is bad practice, well, then there is no point of having a typeless language in the first place tho :-)

  • giggity (unregistered) in reply to Steve_The_Cynic

    That's what she said.

    Unfortunately.

  • Sauron (unregistered) in reply to Vilx

    We don't know in which language the ternary is. In some languages, == is a loose comparison.

    If it is the case, then this ternary could be comparing things of wildly different nature.

    Maybe the accounts variable is an enormous object, and a loose comparison with 1 is unpredictably true or false, depending on the phases of the moon, the mood of the CEO, the weather, and the network latency. Who knows?

  • NotAThingThatHappens (unregistered) in reply to Steve_The_Cynic

    There can be only one truth.

  • Dmitri (unregistered)

    If accounts equals 1, return 1, otherwise return accounts

    Always be yourself, unless you can be a dragon. Then always be a dragon.

  • (nodebb)

    bool result = true;

    result = webServiceClient.GetBoolean(result);

    XmlSerializer.Serialize("c:\file_not_found.txt", result);

    result = XmlDeserializer.Deserialize<bool>("c:\file_found.txt");

    return result == true ? true : !!result;

  • Shiwa (unregistered)

    The ternary may not be a no-op, e.g. in JavaScript where "1"==1 and 1==true evaluate to true… Though that suggests another WTF earlier in the code. And, as with the PHP function, this behavior should be documented (assuming it is intentional).

  • Kleyguerth (github)

    As others have noted, in some languages like Javascript, the ternary might make sense. But thinking about it in a strongly typed language made me actually LOL, Remy's comment was right on point!

    I wonder if it is common practice for companies working with PHP to bait and switch devs like that. Also happened to me: hired as Java dev, work was only PHP. Ran away ASAP.

  • (nodebb)

    The PHP bit looks like input sanitizing. I suspect the string has previously been forced to uppercase.

  • cellocgw (unregistered) in reply to Steve_The_Cynic

    Naaah, it just teaches people to discipline themselves and only us ALL CAPS ALL THE TIME

  • nz (unregistered)

    Reminds me of a format-like program that asks Type "yes" in uppercase letters"...

  • WTFGuy (unregistered)

    They're just trying to make sure you're really paying attention to what you're about to do before you commit to it. ;)

  • (nodebb) in reply to WTFGuy

    “Are you sure you want to delete your character Bobcat? Type Bobcat to confirm.”

  • radarbob (unregistered) in reply to my name is missing
    Comment held for moderation.
  • (nodebb)

    The first one makes me think that accounts == 1 is a special case for certain reasons, and thus was singled out when giving requirements, without realising that it wasn't necessary in this case. The coder has then coded the requirements as they were given, without giving it further thought.

  • (nodebb)

    In the first case there can be implicit type conversions at play. In the second it does standardize to two values, even if some of the transforms may be unexpected. Neither of these are code that is safe to change without understanding the full context.

  • markm (unregistered)
    Comment held for moderation.
  • markm (unregistered)
    Comment held for moderation.

Leave a comment on “Holiday Sample Pack”

Log In or post as a guest

Replying to comment #:

« Return to Article