Hello, I remember something about avoiding the usa...
# codingconventions
o
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
Copy code
if (bla) { 
...
} else  {
...
}
Instead of
Copy code
when(bla) {
true -> ...
false -> ...
}
m
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
I find it way more balanced 🙂
1
o
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
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
Got it this example of
text
and
password
doesn’t really help haha
a
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
how about a compromise that’ll make everyone unhappy?
Copy code
val result = when {
  foo -> "true"
  else -> "false"
}
😂 2
😬 2