9

Spent like 24h trying to figure out why the system does not work. It turns out I forgot to put ternary operator into parentheses. :///

Comments
  • 8
    Serves you right, this is really ugly code.

    Condition expressions without parentheses 💩

    Ternary operator embedded in a condition expression 💩💩💩

    Function calls in condition expressions 💩

    Function calls with side effects in condition expressions ☠☠☠☠☠
  • 0
    @ddephor function calls in conditions is bad? To what extend? Even getter methods? What about something like an array contains. Or stream expressions (in Java)
  • 0
    @gitreflog You can never be sure about side effects and volatility of the result of the function. Additionally it's hard to debug. The more function calls per expression, the harder.

    So I'd better not do it.

    I know it's a common case, but it's better to find another way
  • 0
    @gitreflog I forgot one point. On complex condition expressions functions may or may not be called depending on the expression results.
    With a simple getter it's not an issue, but if there are side effects you get undefined behaviour.

    And I've seen many of those implementations over the years. Often it was intentional, but let's be honest, if there are more than 2 conditions it's not testable.
  • 0
    @ddephor One thing: that is not undefined behaviour. It is very well defined and should work exactly like that
  • 0
    @gitreflog The complexity grows fast in this case. And if it's too complex to document and test the code, you cannot call it defined behaviour.
  • 0
    @gitreflog The complexity grows fast in this case. And if it's too complex to document and test the code, you cannot call it defined behaviour.
  • 0
    @ddephor Wrong. Undefined Behaviour means: the language specs have no information what should happen in this case. But, in 99.9% of languages (Java, Python, C, Javascript) the following is defined

    return (a == b) && functionCall()

    as equivalent to

    if (a == b)
    return functionCall()
    else
    return false
  • 0
    @gitreflog We're not talking about language specifications, it's about implementations.
  • 0
    @ddephor Defined behaviour is not about your implementation, it is whether or not a behaviour is defined in the language specification
Add Comment