I see a “magic” code in <JetBrains/skiko> …:joy:
# random
l
I see a “magic” code in JetBrains/skiko😂
🤣 2
😄 1
🪄 3
p

https://c.tenor.com/HylPDAulWTEAAAAM/star-wars.gif

There is no
when
in kotlin
k
Aside from the artistic looks, why would anyone do
if ("literal" == variable)
in Kotlin when you can do
if (variable == "literal")
even when the variable might be null? (By the way, the expressions 0, 1, etc, look more like Fortran label numbers to me rather than resulting values.)
s
It is probably a defensive habit from languages where assignment is an expression and types can be coerced to boolean implicitly.
if ("literal" = variable)
would be a compile error while
if (variable = "literal")
would be a runtime bug.
k
Thanks. Even when I was a C and C++ developer, I still didn't use Yoda conditions because both the IDE and the compiler gave clear warnings when using
=
instead of
==
.
y
I heard this once from an older developer, but I haven't heard it anywhere else, but "apparently" some early compilers optimized checking against a literal better if the literal was the first argument? It sounds more absurd now that I say it out loud, so idk if there's any truth to it
g
"Yoda conditions", huh TIL