53

Why is the ternary operator such a hated thing? I constantly hear people saying it's less readable, confusing, etc.

I think it's a beautiful, useful, and important operator and I use it constantly wherever readability won't be much affected.

How can you justify a repetitive if/else structure over a ternary, given that you're sure you're not going to put anything else in the ifs?

Whatever happened to DRY?
Whatever happened to KISS?

If those guidelines are what you code by, what's the excuse not to use it?

Because you can't read it as well? Familiarity breeds comprehension and legibility, my dudes.

Comments
  • 23
    I like using ternary operator... As long as it is not nested... Nested ternary operators can be a pain to read.
    Also... If the statements are large, finding a ? And a : is quite evasive haha... But these are probably just what I feel.

    TL_DR;
    It's cool as long as it is not nested and statements are not too huge.
    😉
  • 4
    Yes, that's why I put qualifiers in there. Even yesterday I was accused of trying to "sound smart" (whatever that means) by reducing 5 lines of code to one by using a ternary. People really don't like this operator sometimes!

    Wrote an article about its usefulness a while back, and hopefully it'll get through to some people: https://github.com/densedever/...
  • 4
    ternary is OK, unless you try to nest it, or try to perform multiple statments in it. Short and sweet is good.
  • 10
    Just need to add a few guidelines, like:

    Never do a ternary as a result of a previous ternary result.

    Never do a ternary in a function call. Try and create a variable before the function to store the result.

    Extract multiple conditions for a ternary in to a function. For example:
    var maxValue = ((hour > 5 && minute < 15 && second > 8)) || (username == "you")) ? 360 : 6;
    😱😱😱
    var maxValue = checkSomeRandomCrap(obj) ? 360 : 6;
    😘

    These guidelines definitely help improve the readability of your code if you ever decide to use ternary operators 😉
  • 4
    I love it. It can make some things so much prettier.
  • 3
    I don't mind it in general, but I also almost never use it simply because multi-line ternary is typically bad style and I prefer multi line in order to separate the test condition and the statements; this is friendlier for debugging embedded systems, since most (all?) microcontroller debuggers (I am usually developing on bare-metal ARM) don't support nested breakpoints (or conditional breakpoints).

    It is quite frustrating to debug for 10 minutes stepping through code to reach a specific point in the code that is in a specific state; I then reach a single line if/else or ternary that I need to break on one of the result, to reach the desired state that I need to debug on. Instead I either have to let the routine get called multiple times (10, 20, sometimes hundreds of iterations) until I see the variable change to what I expect or stop debugging, edit the file to use multi line and start over.

    If there's a better way I am all for hearing about it.
  • 3
    It's gold for assignment operations. ;) The code gets so much more readable, in my opinion.
  • 2
    writing if then else is mire readable than ? : or ?? !!. you should rather use the if expression
  • 22
    I like using if statements.
  • 2
    @ac1235 you're telling me that writing

    int a;
    if(conditional) {
    a = val1;
    } else {
    a = val2;
    }

    is more readable than

    int a = conditional ? val1 : val2;
  • 1
    @AngryDev if it is like that...then yes 😄

    Of course you have to know about the syntax though.

    And it is a tradeoff between readability and efficiency. I think the majority of coders will use the ternary wherever possible...why write more code than you have to 😉
  • 2
    I prefer languages where if is an expression, so I say

    let a = if conditional { val1 } else { val2 };

    Or
    let a = if conditional {
    val1
    } else {
    val2
    };
  • 1
    @DrEmann That's one of the small reasons I use Haskell tbh.
  • 0
    I believe ternary operators can be helpful, once you get used to them. If you don't use them often, they are a real pain to read.

    I currently learn php 7, coming from C/C++/C# , and I'm working on a project written by someone else that is littered with ternary operators for assignments, in eloquent SQL and everywhere else.

    I converted them all to if/else today, I can see a lot more clear now.

    Personally I prefer, well structured if/else, even for the cost of temporary variables.
Add Comment