Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@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 = ðŸ˜) -
Pickman6566y@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. -
@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. -
@vlatkozelka kotlin is siccccck you can also do
return try {
doSomeSketchyShit()
A
} catch(e: TooSketchyException) {
B
}
Related Rants
Where's my caviar
joke/meme
legend
if statements
conditions