Cicero
11/28/2020, 2:25 PMval incomingString = "A lot of things that are not interesting but this: ThePath"
when(incomingString){
"The Path" -> something
"No Path" -> something
else -> somethingElse
}
Better exampleAnimesh Sahu
11/28/2020, 2:41 PMsubsequent offered strings
mean?Nir
11/28/2020, 2:48 PMCicero
11/28/2020, 3:11 PMCicero
11/28/2020, 3:12 PMCicero
11/28/2020, 3:13 PMCicero
11/28/2020, 3:13 PMAnimesh Sahu
11/28/2020, 3:14 PMAnimesh Sahu
11/28/2020, 3:14 PMString
?Cicero
11/28/2020, 3:14 PMval incomingString = "A lot of things that are not interesting but this: ThePath"
when(incomingString){
"The Path" -> something
"No Path" -> something
else -> somethingElse
}
Cicero
11/28/2020, 3:14 PMCicero
11/28/2020, 3:14 PMAnimesh Sahu
11/28/2020, 3:14 PMCicero
11/28/2020, 3:14 PMCicero
11/28/2020, 3:15 PMCicero
11/28/2020, 3:15 PMCicero
11/28/2020, 3:16 PMAnimesh Sahu
11/28/2020, 3:16 PMCicero
11/28/2020, 3:16 PMCicero
11/28/2020, 3:17 PMAnimesh Sahu
11/28/2020, 3:30 PM"The Path" in incomingString
?Halex
11/28/2020, 3:55 PMCicero
11/28/2020, 4:06 PMCicero
11/28/2020, 4:27 PMCicero
11/28/2020, 4:27 PMCicero
11/28/2020, 4:28 PMAnimesh Sahu
11/28/2020, 4:28 PMCicero
11/28/2020, 4:28 PMtateisu
11/29/2020, 7:27 PMwhen{ incomingString.endsWith("The Path")-> something ... }
tateisu
11/29/2020, 7:29 PMtateisu
11/29/2020, 7:31 PMtateisu
11/29/2020, 7:33 PMfun foobar(){
val str="..."
fun a(k:String) = str.endsWith(k)
when{
a("1")-> ...
a("2")-> ...
a("3")-> ...
}
}
Cicero
11/29/2020, 8:08 PMTobias Berger
11/30/2020, 11:12 AMwhen
can always be replaced with if/else syntax. Using it with a parameter is just additional sugar if you only need equality or type checks. It's usually also easier to read than a list of else if
blocks.
I'd advise against doing something like this foobar
example. It makes the code a littlie bit shorter, but actually harder to read, because the additional function needs to be read.
If I read "MyString" in incomingString
or incomingString.matches(something)
, I know what's happening and there is no added complexity of an additional local function that doesn't add any functionality.Halex
11/30/2020, 11:17 AMCicero
11/30/2020, 11:19 AMfun foobar(k: String){
fun matches(s:String) = str.contains(s)
when{
matches(k)-> ...
matches(k)-> ...
matches(k)-> ...
}
}
Cicero
11/30/2020, 11:19 AMTobias Berger
11/30/2020, 11:22 AMHalex
11/30/2020, 11:32 AMmatches
, it could be anything significantAnimesh Sahu
11/30/2020, 11:33 AMCicero
11/30/2020, 11:38 AMHalex
11/30/2020, 11:40 AMinline
functions on functions without a lambda argument, the checker tells you that is is useless.
if you meant writing overhead, then it is worth it, because if you have 100 calls and in the future you would want to change the matching logic, it is a pain to change all of them. that's why we do refactoring and abstractions, to make easily modifiable codeTobias Berger
11/30/2020, 11:48 AMin
or do a simple function call, it can very easily be changed for many lines at once (especially it they are all in the same block).
If your check is more complex, it is also a lot more likely to change and extracting it probably makes sense.
Besides: if you have 100 branches in a single when block, please refactor your code!Halex
11/30/2020, 11:52 AMTobias Berger
11/30/2020, 11:53 AMCicero
11/30/2020, 12:46 PMtateisu
11/30/2020, 2:27 PMtateisu
11/30/2020, 2:30 PM