2
HamsterOh
213d

Since i know the operator ? my life changed.

Comments
  • 1
    not really, the ? operator basically just makes the same as an if/else (atleast the compiler does so)

    maybe depending of which language you're talking about...

    `return isSame ? true : false`
  • 0
    Does the ternary operator cause you to "question" your programming choices?

    @s0LA

    It returns a value and allows assignment of the result to a variable. Quite different than if/else. Logically similar, but very handy for conditional assignments.
  • 1
    I use it to reduce lines of codes when you just need a if for 1 thing.
  • 1
    Like:

    myName = value ! = null ? value : "Unknown";
  • 0
    @Demolishun every ternary operator you're using can be replaced with an if/else.
    And the compiler does exactly the same.

    Maybe i just don't get your point? If so, would you mind explaining it to me in more detail, preferably with an example?
  • 0
    int i = a > 1 ? a : 1; // assigns to i

    if(a > 1) i = a; else i = 1; // i has to be explicitly assigned

    Ternary is much more convenient for short conditional assignments.
  • 0
    @Demolishun But in core it is still an if/else.
    I never said it's impractical to use the ternary operator, i just said that it's basically the same as an if/else condition
  • 0
    @s0LA My syntactic sugar is better than your syntactic sugar! 😉
  • 0
    @Demolishun What do you mean by that?

    I mean, it's basically doing the same! Check here for yourself!
    https://godbolt.org/z/ddymoc

    EDIT: Had to edit the source because i forgot that i do a (i+1) so i couldn't use ++i, since it's not the same.
  • 0
    @s0LA This is turning into a wk190 discussion.
Add Comment