But why?:
Maybe it's a leftover from Objective-C, but I — still — consider readability way more important than brevity. Code is not a limited resource, you can be as verbose as you want to be.
There's no price for the shortest code written. And this also applies to if
, else
, and ?:
, the so called ternary operator. I trip over ?:
every single time. I read code from top to bottom, from left to right and encountering a ?
after a condition means that I have to jump back to the condition. This is just like my brain works. If there's an if
, it's clear from the beginning.
It feels like someone was too lazy to do a proper if else
in favor of brevity, not caring about future programmers reading their code. Selfishly valuing their own time (or the time and money of their shareholders) over the time of other people. But code is more often read than written.
This brings us to SwiftUI. Due to reasons — and ternary operator-lovers, obviously — you are supposed to sprinkle ?:
literally everywhere in your code.
Wanna show a different image depending on a condition?
Image(condition ? .a : .b)
Wanna apply a different scale depending on a condition?
.scaleEffect(condition ? 2.0 : 1.0)
Wanna display a different text depending on a condition? You guessed it:
Text(condition ? "A" : "B")
TIHI. The hurtful truth is that if else
also looks ugly in SwiftUI-code, and so I trip every single time and I cringe when I have to write ?:
. Or SwiftUI at all.