r/badcode • u/friendlyChickenDog • Oct 05 '21
other language Ternary madness in a Flutter library. I can understand why the golang devs got rid of it.
570
Upvotes
r/badcode • u/friendlyChickenDog • Oct 05 '21
180
u/sayqm Oct 05 '21
Ternary itself is fine, the issue here is clearly the incompetent developers that didn't refactor any of it over time
165
u/[deleted] Oct 05 '21
I love it when code looks like Michael Jackson doing the antigravity lean
73
u/dwengs Oct 05 '21
My rules for ternary:
36
u/nath1as Oct 05 '21 edited Oct 05 '21
taking features out of a language doesn't prevent people being dumb
34
u/Ra1d3n Oct 05 '21
How to make a hash map with extra steps and less maintainability.
12
u/random_runner Oct 05 '21
Also "canTwoLevel" is in there twice.
12
u/[deleted] Oct 05 '21
(cough) switch
33
u/morganasreddit Oct 05 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/edvardsenrasmus Oct 05 '21
This really isn't a problem with ternary operators, but rather misusing the tools at your disposal.
Here, we are checking against one condition, and wish to set one variable in accordance with what check evaluates true first.
This is very cleanly solved by a switch-statement or switch-expression, depending on your language.
Another bad way to solve this would be with if-else-if-else-statements.
7
u/WpGgs Oct 05 '21
In addition, it checks for “canTwoLevel” two times.
11
u/R3D3-1 Oct 05 '21
The irony? The only actual madness is the indentation scheme.
This is just a regular case of
The indentation scheme is essentially like insisting that every
elsemust be a block -- includingelse if.4
u/timelessblur Oct 05 '21
Ternary is great. The use here is poor. This is a perfect example of something to refactor into a switch statement. I know I have a habit that most of the time on a enum i default to a switch statement very fast. Big time if I expect it to have more than if check.
4
u/ciaisi Oct 05 '21
Guys! I just learned ternary operators! I'm going to replace all of my if else statements with them!
ALL OF THEM!
6
u/[deleted] Oct 05 '21
You can’t and shouldn’t cater to or try to fix bad programmers. There is no amount of baby proofing to fix stupid.
Bad programmers gonna bad program, you just add the features the good programmers need
3
u/UltraPoci Oct 05 '21
I like Rust's approach to if else, which can return a value from its branches, which means that you can write an if else statement and use it like you would you use a ternary operator. It's a little more verbose, sure, but it's a lot clearer.
3
u/[deleted] Oct 05 '21
Repeat after me: Pattern Matching.
3
u/Voltra_Neo Oct 05 '21
Ternary is fine, abusing it isn't. Clearly this should be a switch
3
u/d9viant Oct 05 '21
This can be refactored pretty easily
2
u/stone_henge Oct 05 '21
Aah, the satisfaction of turning something that really shouldn't be a single expression into a single expression
1
u/JonnyBoy89 Oct 05 '21
Perfect example of a use case for a switch statement. The cognitive complexity of this ternary is soooo unnecessary
1
u/virteq Oct 05 '21
btw, you can use CircularProgressIndicator.adaptive() instead of checking the platform.
1
u/RichCorinthian Oct 05 '21
Flutter is heavily influenced by React, so bad Flutter looks like bad React.
0
u/_pelya Oct 05 '21
At least it's indented properly
0
u/[deleted] Oct 05 '21
[deleted] — view deleted comment