https://kotlinlang.org logo
Title
o

orafaaraujo

11/18/2020, 6:05 PM
Hello, I remember something about avoiding the usage
when
when you can go with a simple
if
But I can’t find it now… Does anyone knows where is it?
e.g.: do
if (bla) { 
...
} else  {
...
}
Instead of
when(bla) {
true -> ...
false -> ...
}
m

Marc Knaup

11/18/2020, 6:12 PM
When used as an expression I often find the latter easier to read.
👀 1
Thank you, @Marc Knaup I think
when
really charm as well, I was just wondering in that specific case
m

Marc Knaup

11/18/2020, 6:17 PM
I find it way more balanced 🙂
1
o

orafaaraujo

11/18/2020, 6:20 PM
this really makes me think which one would fits better…
In your case, following the link, I would go with
if (revealsPassword) text else password
and using `import InputType.text`/`.password`
m

Marc Knaup

11/18/2020, 6:23 PM
That’s definitely something I don’t do. I don’t want to have all these symbols flying around without qualification. Also, there’s a local variable called
password
.
o

orafaaraujo

11/18/2020, 6:26 PM
Got it this example of
text
and
password
doesn’t really help haha
a

alex

11/18/2020, 7:37 PM
I find
when
easier to read in assigning expressions, especially if condition/action is not short. Basically, if
if
cannot be expressed as
val x = if (flag) then y else z
, I'd prefer
when
. Few weeks ago I found very big project with code linting extremes, such as "do not use if at all, use when instead". Can't remember if this rule was applied for all
if
constructions or just an assinging expression though.
s

Shawn

11/18/2020, 8:00 PM
how about a compromise that’ll make everyone unhappy?
val result = when {
  foo -> "true"
  else -> "false"
}
😂 2
😬 2