• Ziplodocus (unregistered)

    Count(comments) = 0 ? "Frist" : "Discourse is the real WTF"

  • cyborg (unregistered) in reply to Ziplodocus
    Ziplodocus:
    Count(comments) = 0 ? "Frist" : "Discourse is the real WTF"

    An ingenium example of the frist reason to use ternaries.

  • nerd4sale (cs) in reply to Ziplodocus
    Ziplodocus:
    Count(comments) = 0 ? "Frist" : "Discourse is the real WTF"
    Discourse?WTF:Thrid!
  • Smug Unix User (unregistered)

    Stop trying to make discourse happen.

  • CigarDoug (unregistered)

    Personally, in all my years of reading the daily WTF, I have had zero use for Discourse.

  • my name (unregistered)

    dis·course, Latin - from the verb discurrere, from dis- 'away' + currere 'to run'.

    So it originally meant 'run away'? See, even the Ancient Romans were trying to warn us against using it!

  • n/a (unregistered)

    Discourse in an ancient African word meaning "I'll spam your browser history with thousands of meaningless links".

  • Zecc (disco)
    Comment held for moderation.
  • aliceif (disco)

    Well, ok. This code sample clearly shows how bit flags shouldn't be read.

    However, the introduction text is something I vehemently disagree with. The ternary operator is a great tool to make code more readable by dropping unnecessary syntactic verbosity. Not to forget, it gives you a LISP-style if in C-Style languages without any parenthesis hell!

  • Zecc (disco)

    Btw, that line of code doesn't seem to be doing anything. It's just an expression that returns a string.

    Are they missing an assigment?

  • dkf (disco)

    I knew someone who wrote a functional program in C with just one function — main() — and just one statement — return. It was some of the most flagrant abuse of ternaries and comma operators that I've ever seen, and that was before you got into the realms of all the undefined behaviour with which it was strewn.

    It edited DOS boot sectors (as far as I can remember; it was a long time ago).

  • peppernut (disco)

    The ternary abuse is one thing ... but I cannot figure out the bit operators.

    At some point you don't have a bitfield anymore, you functionally have an integer. This seems like a really bad way to write "switch (FailSafe & 0x0F)"

  • TGV (disco) in reply to peppernut
    peppernut:
    The ternary abuse is one thing ... but I cannot figure out the bit operators.
    I think that for each bit they concatenate strings: bit 0 is 'Detection zones staying in a given state', bit 1 is 'Bad visibility', etc. So the first condition tests for all bits and then returns the string with all error messages. Basically, an index in array of strings would have done the same, but more readable.

    PS Note that it isn't C.

  • faoileag (disco)
    the code:
    `...0?'Bad configuration':'Unknown'`
    Of course the original developer of that long chain of ternary operators is doing it wrong!

    ...0?'Bad configuration':'FILE_NOT_FOUND'

    There. Much better!

  • Zecc (disco) in reply to aliceif
    aliceif:
    However, the introduction text is something I vehemently disagree with. The ternary operator is a great tool to make code more readable by dropping unnecessary syntactic verbosity.
    I agree with your vehement disagreement. This code would be nearly as WTFy if it was written with `if` statements on one line.

    You could increase readability tremendously just by properly formatting and rewriting the conditions (I'm assuming this is a language with sane precedence and associativity — ie, not PHP — and getting rid of the extraneous parens too):

    ``` FailSafe == 0 ?     'No technical alarms' :      FailSafe & (1|2|4|8) == (1|2|4|8) ?     'Detection zones staying in a given state; Bad visibility; Initialization; Bad configuration' :      FailSafe & (1|2|4) == (1|2|4) ?     'Detection zones staying in a given state; Bad visibility; Initialization' :      FailSafe & (1|2|8) == (1|2|8) ?     'Detection zones staying in a given state; Bad visibility; Bad configuration' :      FailSafe & (1|4|8) == (1|4|8) ?     'Detection zones staying in a given state; Initialization; Bad configuration' :      FailSafe & (2|4|8) == (2|4|8) ?     'Bad visibility; Initialization; Bad configuration' :      FailSafe & (1|2) == (1|2) ?     'Detection zones staying in a given state; Bad visibility' :      FailSafe & (1|4) == (1|4) ?     'Detection zones staying in a given state; Initialization' :      FailSafe & (1|8) == (1|8) ?     'Detection zones staying in a given state; Bad configuration' :      FailSafe & (2|4) == (2|4) ?     'Bad visibility; Initialization' :      FailSafe & (2|8) == (2|8) ?     'Bad visibility; Bad configuration' :      FailSafe & (4|8) == (4|8) ?     'Initialization; Bad configuration' :      FailSafe & 1 == 1 ?     'Detection zones staying in a given state' :      FailSafe & 2 == 2 ?     'Bad visibility' :      FailSafe & 4 == 4 ?     'Initialization' :      FailSafe & 8 == 8 ?     'Bad configuration' :      'Unknown'; ```

    Compare to the same conditions but with if statements instead:

    ``` if (FailSafe == 0){     return 'No technical alarms'; } else if ( (FailSafe & 1) != 0 && (FailSafe & 2) != 0 && (FailSafe & 4) != 0 && (FailSafe & 8) != 0 )}     return 'Detection zones staying in a given state; Bad visibility; Initialization; Bad configuration'; } else if( (FailSafe & 1) != 0 && (FailSafe & 2) != 0 && (FailSafe & 4) != 0 ){     return 'Detection zones staying in a given state; Bad visibility; Initialization'; } else if( (FailSafe & 1) != 0 && (FailSafe & 2) != 0 && (FailSafe & 8) != 0 ){     return 'Detection zones staying in a given state; Bad visibility; Bad configuration'; } else if( (FailSafe & 1) != 0 && (FailSafe & 4) != 0 && (FailSafe & 8) != 0 ){     return 'Detection zones staying in a given state; Initialization; Bad configuration'; } else if( (FailSafe & 2) != 0 && (FailSafe & 4) != 0 && (FailSafe & 8) != 0 ){     return 'Bad visibility; Initialization; Bad configuration'; } else if( (FailSafe & 1) != 0 && (FailSafe & 2) != 0 ){     return 'Detection zones staying in a given state; Bad visibility'; } else if( (FailSafe & 1) != 0 && (FailSafe & 4) != 0 ){     return 'Detection zones staying in a given state; Initialization'; } else if( (FailSafe & 1) != 0 && (FailSafe & 8) != 0 ){     return 'Detection zones staying in a given state; Bad configuration'; } else if( (FailSafe & 2) != 0 && (FailSafe & 4) != 0 ){     return 'Bad visibility; Initialization'; } else if( (FailSafe & 2) != 0 && (FailSafe & 8) != 0 ){     return 'Bad visibility; Bad configuration'; } else if( (FailSafe & 4) != 0 && (FailSafe & 8) != 0 ){     return 'Initialization; Bad configuration'; } else if( (FailSafe & 1) != 0 ){     return 'Detection zones staying in a given state'; } else if( (FailSafe & 2) != 0 ){     return 'Bad visibility'; } else if( (FailSafe & 4) != 0 ){     return 'Initialization'; } else if( (FailSafe & 8) != 0 ){     return 'Bad configuration'; } else {     return 'Unknown' }
    </blockquote>
    Neither is a good replacement for a  `switch` statement, a message array or string concatenation.
    
    <i>Edit: some code formatting mistakes</i>
    <i>Edit 2: added "associativity" in response to VinDuv's post</i>
    <hr>Filed under:<div align="center"><kbd>Boredom level: <meter min="1" value="7" max="10" title="7 out of 10">7 out of 10</meter></kbd></div>
    
  • faoileag (disco) in reply to aliceif
    aliceif:
    However, the introduction text is something I vehemently disagree with.
    +1
    Mark Bowytz:
    I have had zero use of ternary operators ... The only practical application of ternary operator is to either intentionally obfuscate your code... ... Case in point, the single line of code

    These remarks translate into the following logic:

    "I have not used feature X." "I have seen an example of feature X abused." "Ergo feature X must be bad".

    Which might be a wtf in its own right.

  • VinDuv (disco) in reply to Zecc
    Zecc:
    I'm assuming this is a language with sane precedence — ie, not **C or any later language which copied its rules**
    FTFY
  • CarrieVS (disco)

    TRWTF is not the ternary operators instead of if-else (although ternary operators for complex if statements, especially nested ones, are certainly a wtf). It's that there's four different things representing known problems, any combination of which could be non-zero, and it tests for each possible combination - a total of 16 conditions, not counting all zero and anything other than those four being non-zero, even though it always does the same thing for, e.g. FailSafe&2 != 0 regardless of how many others there are.

    if (FailSafe == 0){
        return 'No technical alarms';
    }
    else {
        message = '';
    
        if ( (FailSafe & 1) != 0 ){
            message .= 'Detection zones staying in a given state';
        }
        [...etc]
    
        if (message == ''){
            message = 'Unknown';
        }
    
        return message;
    }
    

    Edit: slight improvement

  • Zecc (disco) in reply to VinDuv
    Comment held for moderation.
  • Steve_The_Cynic (disco) in reply to aliceif
    aliceif:
    The ternary operator is a great tool to make code more readable by dropping unnecessary syntactic verbosity.
    Terseness is not necessarily a virtue. As frequently used, the ternary is an effective tool for hiding if() statements or, as we see here, a switch-case. But hey, the worst ternary abuse I ever saw was this:
    if ( condition1 )
       condition2 ? some_var = some_expression : 0;
    else
       something_else;
    

    Some lazy sod couldn't be bothered to add the braces when converting the inner if() from a simple assignment.

    My main argument for avoiding the ternary is that it is so subject to abuse that allowing it outside of some strictly regulated circumstances is a really bad idea, sort of like how overt goto should be handled. Examples of reasonable ternaries are where you want to pass this or that parameter (depending on some condition) to a function without having to duplicate the rest of the function call. Unreasonable ones include constructions like in this WTF, but also things like this:

      (some_condition ? function1 : function2)(some, long, argument, list);
    

    That is, call the appropriate one of these functions with the same argument list.

    My main objection is not so much that ternaries hide conditions / if()s / switch-cases as that when reading the code I have to stop and decode them to work out what they are trying to do - the syntax is excessively quiet and the rules of precedence are a bit flaky across languages.

  • rep_movsd (disco)

    If this was a sane language that used C style precedence rules, you could write this very clearly as :

      FailSafe==0 ? "No technical alarms":
      FailSafe & 1 && FailSafe & 2 && FailSafe & 4 && FailSafe & 8 ? "Detection zones staying in a given state; Bad visibility;Initialization; Bad configuration":
      FailSafe & 1 && FailSafe & 2 && FailSafe & 4 ? "Detection zones staying in a given state; Bad visibility; Initialization":
      FailSafe & 1 && FailSafe & 2 && FailSafe & 8 ? "Detection zones staying in a given state; Bad visibility; Bad configuration":
      FailSafe & 1 && FailSafe & 4 && FailSafe & 8 ? "Detection zones staying in a given state; Initialization; Bad configuration":
      FailSafe & 2 && FailSafe & 4 && FailSafe & 8 ? "Bad visibility;Initialization; Bad configuration":
      FailSafe & 1 && FailSafe & 2 ? "Detection zones staying in a given state; Bad visibility":
      FailSafe & 1 && FailSafe & 4 ? "Detection zones staying in a given state; Initialization":
      FailSafe & 1 && FailSafe & 8 ? "Detection zones staying in a given state; Bad configuration":
      FailSafe & 2 && FailSafe & 4 ? "Bad visibility; Initialization":
      FailSafe & 2 && FailSafe & 8 ? "Bad visibility; Bad configuration":
      FailSafe & 4 && FailSafe & 8 ? "Initialization; Bad configuration":
      FailSafe & 1 ? "Detection zones staying in a given state":
      FailSafe & 2 ? "Bad visibility":
      FailSafe & 4 ? "Initialization":
      FailSafe & 8 ? "Bad configuration":"Unknown";  
    

    Brackets are superfluous as are !=0 conditions

  • TGV (disco) in reply to rep_movsd
    rep_movsd:
    If this was a sane language that used C style precedence rules, you could write this very clearly as
    Ok, now rewrite "FailSafe & 1 && FailSafe & 2 && FailSafe & 4 && FailSafe & 8" to "FailSafe == 15", etc., and you'll see that it's just a list from 0..15.
  • Nagesh (disco)

    Loving complexity of the operator here.

  • xaade (disco) in reply to faoileag
    faoileag:
    +1

    "I have had zero use of ternary operators"

    These remarks translate into the following logic:

    "I have not used feature X."

    I read it as the author meant to type "I have had zero use for ternary operators".

    Which makes infinitely more sense.

  • antiquarian (disco) in reply to xaade
    Comment held for moderation.
  • rep_movsd (disco) in reply to TGV

    My exercise was to use only delete :smile:

  • mruhlin (disco) in reply to CarrieVS

    Depending on the use case, you might care about the performance of string concatenation in frequently-occurring conditions.

  • xaade (disco) in reply to antiquarian

    Not saying there IS zero use for...

    Just saying the phrase

    have had zero use of

    is less likely meant than

    have had zero use for

  • abarker (disco) in reply to antiquarian
    Comment held for moderation.
  • antiquarian (disco) in reply to xaade

    I understood you. I'm saying that going from "had zero use of" to "had zero use for" isn't much of an improvement.

  • faoileag (disco) in reply to abarker
    abarker:
    if (testValue == true)
    {
       setvalue = 0;
    }
    else
    {
        setvalue = 42;
    }
    

    Personally, I prefer the ternary operator version of that:

    setvalue = (testValue == true) ? 0 : 42;
    

    And if you are setting a hitherto undeclared variable using the ternary form is even more elegant:

    my $answer = 0;
    if ($book->isHHGTTG()) {
      $answer = 42;
    }
    ```
    looks clumsy against:
    
    ```perl
    my $answer = $book.isHHGTTH() ? 42 : 0;
    ```
    
  • Zecc (disco) in reply to mruhlin
    mruhlin:
    Depending on the use case, you might care about the performance of string concatenation in frequently-occurring conditions.
    ``` {     /* .... */  "No technical alarms",     /* ...1 */  "Detection zones staying in a given state",     /* ..2. */  "Bad visibility",     /* ..21 */  "Detection zones staying in a given state; Bad visibility"     /* .4.. */  "Initialization",     /* .4.1 */  "Detection zones staying in a given state; Initialization",     ...     /* 8421 */  "Detection zones staying in a given state; Bad visibility; Initialization; Bad configuration" }[FailSafe]``` (substitute `{ }` with `[ ]` depending on language)
  • Steve_The_Cynic (disco) in reply to abarker
    abarker:
    ```c# if (testValue == true) { setvalue = 0; } else { setvalue = 42; } ```

    Comparing boolean-valued things to true is a form of insanity. It isn't (normally) a problem in languages like C# or Java, but in C it is particularly dangerous - lots of values are (by the normal definitions of the language) true, but only one of them is equal to TRUE. All the other non-FALSE values are simultaneously true (not equal to FALSE) and false (not equal to TRUE). In C++ it is dangerous for a slightly different reason. I might write if ( k == true ) but if k is not a bool, I will have problems because C++ will silently coerce true to 1 before comparing it to k, rather than the other way around.

    No, in general, you should compare against false as in if ( k != false ) or even just not do the comparison, if ( k ).

    For real fun, though, consider the sinners who write an "operator bool()" member function in a C++ class, where that class might reasonably be used as a key in an STL container such as map or set. The map becomes effectively a map<bool, OtherThing> and therefore holds only two values. Member operator bool() is a favorite of people who are creating smart pointer objects. They should convert to a non-useful pointer instead: operator class ZogShallRule *() where ZogShallRule is defined nowhere, or even declare ZogShallRule as a private inner class:

    class DodgySmartPointer
    {
      private:
        class ZogShallRule;
    
      public:
        operator ZogShallRule *() const { return reinterpret_cast<const ZogShallRule *>(this); };
    ...
    };
    

    You can still use DodgySmartPointer variables nakedly in if() and while() conditions ( if( dsp ) ) but this method doesn't cause havoc in collections.

  • antiquarian (disco) in reply to Steve_The_Cynic
    Steve_The_Cynic:
    Comparing boolean-valued things to true is a form of insanity. It isn't (normally) a problem in languages like C# or Java, but in C it is particularly dangerous - lots of values are (by the normal definitions of the language) true, but only one of them is equal to TRUE. All the other non-FALSE values are simultaneously true (not equal to FALSE) and false (not equal to TRUE). In C++ it is dangerous for a slightly different reason. I might write if ( k == true ) but if k is not a bool, I will have problems because C++ will silently coerce true to 1 before comparing it to k, rather than the other way around.

    C and C++ are dangerous. We've had type-safe alternatives for years.

  • Maciejasjmj (disco) in reply to Steve_The_Cynic
    Steve_The_Cynic:
    It isn't (normally) a problem in languages like C# or Java, but in C it is particularly dangerous - lots of values are (by the normal definitions of the language) true, but only one of them is equal to TRUE.

    Yeah, and then you spend two hours debugging your program before you notice that if (P1IN & 0x80 == TRUE) doesn't really do what you'd want it to. Twice. Been here, done that.

    And ternaries are insanely useful. Let's say you have f(string x, int y), and you want to pass a "YES"/"NO" and 1/0 value depending on some conditions. Would you rather write:

    if (a == 0 && b < 42)  f("NO", 1);
    else if (a != 0 && b < 42) f("YES", 1);
    else if (a == 0 && b >= 42) f("NO", 0);
    else f("YES", 0);
    

    or

    f( a == 0 ? "YES" : "NO", b >= 42 ? 0 : 1 );
    

    ? To me, it's pretty much a no-brainer.

    antiquarian:
    C and C++ are dangerous. We've had type-safe alternatives for years.

    When doing low-level stuff, it's sometimes useful to just hack away without the regard for type safety. As long as you know what you're doing, that is.

  • shmosel (disco) in reply to Steve_The_Cynic

    In a way, it's even more dangerous with type-safe languages like Java and C#, where accidentally using a single equals sign in an if statement would normally cause a compile error, unless you happen to be comparing a boolean variable to a boolean literal.

  • Maciejasjmj (disco) in reply to shmosel
    shmosel:
    unless you happen to be comparing a boolean variable to a boolean literal.

    Which you shouldn't be doing in the first place.

  • shmosel (disco) in reply to Maciejasjmj

    My point precisely.

  • Captain (disco) in reply to Maciejasjmj

    Here's another example of useful ternaries. They're nested, even! Before Perl 5.12 (I believe), Perl didn't have switch or case. If you wanted to switch, you would have to do:

     ( case1 ? result1
     : case2 ? result2
     : case3 ? result3
     : case4 ? result4 
     : noMatch()
     );
    

    This should not be hard to read. It is a very informative structure.

  • shmosel (disco) in reply to Captain

    Your last line seems to be a binary operator.

  • Maciejasjmj (disco) in reply to Captain
    Captain:
    This should not be hard to read. It is a very informative structure.

    I always keep reading ternaries as questions. Something like "Case1? Yes? Ok, then result1." etc. It's pretty intuitive, in a way.

    I also like C#'s x ?? somevalue - basically, a stripped-down x == null ? somevalue : x. Another one of the more useful statements.

  • Blah (disco)

    Sure the code is messed up. However, another WTF is someone submitting code who may have been stuck in their ways enough to utter this opinion: "The only practical application of ternary operator is to either intentionally obfuscate your code or use it as a soapbox to brag about how "l33t" you are.".

    At least here it's a pain to read through, but everything is clear past the formatting. If this had been something along the lines of a ridiculous lambda expressions using implied data types, then I might have agreed (some people shouldn't be allowed to have ReSharper... :-P ).

  • Captain (disco) in reply to shmosel

    Oops, fixed. :)

  • shmosel (disco) in reply to Maciejasjmj

    Or JS's ||

  • Captain (disco) in reply to Maciejasjmj

    I always keep reading ternaries as questions. Something like "Case1? Yes? Ok, then result1." etc. It's pretty intuitive, in a way.

    Exactly. And under that reading, : is "exclusive or". Haskell's pattern guards look like:

    abs :: Num a => a -> a
    abs x | x > 0 = x
          | x <= 0 = -x
    

    They've got almost the same amount of noise (very little to none)

    I also like C#'s x ?? somevalue - basically, a stripped-down x == null ? somevalue : x. Another one of the more useful statements.

    That looks like a prism combinator. Does C# have a generalized framework for constructing prisms?

  • darkmatter (disco) in reply to Zecc

    I would bet that in the code it actually is done like you posted, all spaced out and legible. Then either the submitter or Mark removed the whitespace and newlines to make it extra WTFy. Or a codeshrink utility (if it's an interpretted language) removed all the whitespace and newlines.

    By the way, nice article, that massive ternary mess stands alone (even if it had been whitespaced properly, it's still nasty) and doesn't need a 20 page backstory. Well done.

  • chubertdev (disco) in reply to faoileag
    faoileag:
    I have not used feature X.

    And this is where your wrong assumption undermines your argument. He's saying that he hasn't had a situation where it's useful, not that he hasn't used it.

  • chubertdev (disco) in reply to abarker
    Comment held for moderation.
  • ben_lubar (disco) in reply to chubertdev
    setvalue = 42;
    if (testValue)
        setvalue = 0;
    
  • aliceif (disco) in reply to ben_lubar
    setvalue = testvalue << 1 | testvalue << 3 | testvalue << 5
    

    Filed under: Optimization!

Leave a comment on “One Bad Ternary Operator Deserves Another”

Log In or post as a guest

Replying to comment #:

« Return to Article