86
Comments
  • 7
    On the contrary, I found the second one appaling and peasantish.
  • 12
    That useless else tho
  • 12
    return (condition && A) || B;
  • 3
    @mjones44 what if a is 1, and b is a kitten?

    I don't get why people hate ternaries, they are amazingly readable and succinct when dealing with return values or variable assignments. I even don't mind them nested 1-2 levels deep, as long as some parentheses are used.

    When executing more complex statements, I'd go with something like:

    if (condition) { doSomething(); return stuff;}

    doSomethingElse(); return stuff;

    Which demonstrates why "else" is pretty much a useless language construct.

    (sorry, devrant and indents = 😭)
  • 5
    @bittersweet it's both a matter of personal preference and one of readability. Sometimes ternary get embedded in complex lines of code and this leads the reader to try to understand the whole line in one go when "proper" if and else would transmit the logical structure and allow for easier comprehension and validation. I don't hate ternary operators but I believe that they should be used sparingly because I see them as just an excuse ro write code in a single line (which is great if the resulting line is short and simple). For example with some boolean flags you could refactor it in a single great while loop of one line.

    Also else is codified by the c mpiler differently than if so it has a dignity. It is not necessary from a logical point of view but it's still easier than to write if(!condition) and somewhat less prone to typos so I would include it in my ideal language.
  • 4
    @bittersweet I was just trolling. JS sucks ass.
  • 3
    @bittersweet else is not useless. what if you're not returning from the if?
  • 3
    @mjones44 If you do not return from the if, then your method usually does more than one thing, which is too much.

    I'm not saying my code is free from else statements... But they are rare.

    @Pickman When I have doubts about readability, I usually ask my girlfriend to explain my code to me.

    I try to design the flow of my code & method/variable names in such a way that it reads almost like English sentences.
  • 3
    @bittersweet I like that approach. I should try it too.
  • 1
    @jOkEr-jAsE A very weird thing to say to you clone account , eh?
  • 0
    @vlatkozelka kotlin is siccccck you can also do

    return try {
    doSomeSketchyShit()
    A
    } catch(e: TooSketchyException) {
    B
    }
Add Comment