30

always confused between If...else & ternary operator...😅

Comments
  • 14
    The one reason I don't use ternary is because I can never remember the fucking syntax
  • 2
    Hate ternary op
  • 14
    Damnit I love ternary :(
  • 2
    @sebach1 @lxmcf don't hate because ternary is faster than if else.
    condition ? true case : false case; 😅
  • 1
    @loganlehman yes i am always using ternary operator. 🙂
  • 5
    @ParthRaval It isn't executed faster, it means the exact same thing just with a different syntax. You probably mean shorter, but if you know anything about code quality you also know that compactness isn't a feature on its own. Conciseness is.
  • 1
    @Lor-inc I assume it isn't faster as it is evaluated to the same bytecode? I never use it because I can never remember how to write it for the life of me, and it's hard to read.

    and since we're on that topic, is there actually a way to improve the speed of conditional statements?
  • 1
    @wellyeamaybenot 0 message?
    xD
  • 2
    @ParthRaval I hate when langs give the tools to do something in two diff ways with the exact same result. It isn't consistent for me.
    Then, the ternary op is like 'use it if this is short and simple'. So, wtf does short or simple exactly mean?
    Is like more complexity on a language for skipping, what... two keystrokes?
  • 0
    Too many if else is a good indication of code smell.
  • 0
    @sebach1 You *hate* when there are multiple ways to solve a problem? Get out of comp sci immediately.
  • 0
    @Lor-inc It is downloaded faster, and therefore executed faster. Theres a reason good minifiers convert all if/else blocks to ternaries where it is possible to do so.
  • 0
    @lxmcf It is just the true condition followed by the false. Start with the question mark because the condition is like asking a question. Is this true? YES : NO. Not difficult, only the order of 2 character to remember. What part do you forget?

    Also, if you use VS Code you can just create a snippet to make a ternary. I have a snippet configured to create the ternary structure where I can tab through the elements of it if I type "tern" then tab as it pops up at the top of my suggestion list.
  • 2
    @sebach1 Further, ternaries are not identical to an if else block In terms of how you can use them. You can't use if else statements inside the parameters of a function call, for example, but you can use ternaries in that way.
  • 1
    @smirving HAHAHAHAH wtf. I hate when the same tool have two diff ways to do **exactly** the same.
    It goes in the opposite way of what a tool is desired to be.
  • 2
    @sebach1 Well get over it because all major languages have multiple "tools" that are similar with small variations in their use and syntax. I can think of probably a hundred examples off the top of my head. It is incredibly common and learning the tiny differences in their proper use is an important skill to have as a programmer.

    If else and ternaries are not exactly the same thing though. You maybe should learn more about them before issuing asinine complaints.
  • 0
    @smirving you are talking about one single lang. There are langs that allows it.
    Anyway, I think that love 4 "wrappinness" is the seed of complicated design and 'oh my software is too complex I'm too intelligent'.
    Just assign it in a var. Under the hood, the machine will do the same. And a human will thanks u
  • 0
    @smirving nah there are langs that are designed to be consistent. And, anyway, if Javascript rules the industry that doesnt mean that I need to have its principles.
    C does not. Go does not. Haskell does not.
  • 0
    @sebach1 You can barely construct a sentence I cannot imagine being a human reading your code.
  • 1
    @sebach1 You're telling me C, Haskell, and Go only have one single way to do anything and there are no similar but synactically different ways to do anything in those languages?
  • 0
    @smirving lmao thx for the insult.
    No, obviously those have multiple ways to do some things. But that's not the rule.
  • 1
    Ternary operator is a good case study of the compromise between complexity and skill-cap

    Sure it might be confusing at first but you really miss it after using the language for 5 years
  • 1
    @smirving If minifiers convert it then it's not faster than writing an if-else and minifying it.

    Doing branching in the middle of an expression is literally an example for my point, that it's bad because it promotes unreadable code.

    Expressions are units of code and we generally assume that every function that's named in an expression is executed when the expression is evaluated. If this isn't the case then it should be multiple statements, so we can set breakpoints and generally think of the code in terms of lines.

    Code being hard to understand isn't a feature and less SLOC doesn't mean less complexity.
  • 2
    Love ternary, hate nested ternary
  • 0
    @gazza101uk there's nested ternarry?!?
  • 0
    @mdude72 as another tenary within the else. It’s just unreadable but you can do it
  • 2
    @mdude72 Of course there is, it's just an expression, you can combine it all you want. That's why people despise it, it doesn't force you to stop at some point and just use a variable.
Add Comment