<https://blog.frankel.ch/even-more-readable-code-w...
# feed
d
Hi and a huge thanks for your blog! Its a nice exercise but to be honest I would ding it in a code review. The resulting code need in my opinion much more "brain processing power" to unserstand than the simple if else. There is too much happening in the lines of code. Use some private methods
Copy code
if(..) {
 doSomething()
} else if(...) {
 doSomethingElse()
}
and you are good to go and will understand it even after you were away from the code for some time
đź’Ż 5
On top of it you loose a lot of flexibility. You can rewrite the code once there is a case in the future where the signatures of the "do" methods differ
n
Hi and a huge thanks for your blog!
thanks 🙂
You can rewrite the code once there is a case in the future where the signatures of the “do” methods differ
if believe this is pretty clear if you don’t want to go the full way on the opposite, i would advise to start with the full map things, and only then revert one level back if the signatures start diverging
in all cases, i appreciate your input 👍
d
Its really nice to talk about this stuff! 🙂 For example the one dimensional mapping ("A" to 1) I find a really great idea. Only once things go beyond this I personally find it hard to grasp. Its really a fine line in programming!
n
Its really a fine line in programming!
yep, i wouldn’t bet any part of my anatomy on it
t
I don't get it. Why would you put your stuff in a map, when, instead, you could easily write
Copy code
when(input) {
  "A" -> 1
  "B" -> 2
  "C" -> 3
}
? And taking that further, imagine a scenario where you'd be tempted to use a
sealed class
or an
enum
as map keys? Why give up on the exhaustiveness check?
👍 3
a
yeap, it sounds a bit like bringing java tricks into kotlin I was doing similar stuff in C#, but in Kotlin
when
is as expressive, so map doesn’t bring anything to the table
n
i believe it does if you have multiple clauses to check you might try yourself with 2
&&
it’s imho much harder to read
a
Copy code
when (var1 to var2) {
"a" to "b" -> …
"a" to "c" -> …
}
✔️ 1
👍 1
and of course you are not limited to pair, you can use any other data class. In a domain where this kind of code makes sense, probably there is a better name than Pair for the combination of those two keys
n
ah, ok, you still match on
Pair
i understand