diego-gomez-olvera
07/20/2023, 3:09 PMwhen
? I tend to use Unit
but I don't recall if there is an official convention.
enum class Type { A,B,C }
1️⃣
when (type) {
A -> doA()
B -> doB()
C -> Unit // No-op
}
2️⃣
when (type) {
A -> doA()
B -> doB()
C -> {} // No-op
}
I guess that typealias
could be used as well, but it seems overkillephemient
07/20/2023, 3:15 PMfun noOp() {
// no-op
}
instead of = Unit
ephemient
07/20/2023, 3:15 PMephemient
07/20/2023, 3:16 PMTgo1014
07/20/2023, 3:38 PMUnit
I hate the auto formating for `{}`😆Joffrey
07/20/2023, 11:35 PMUnit
for the consistency with the other non-block expressions. I basically eliminate all braces from when
expressions this way, as it's less noise for parsing with the eyes IMO.pablisco
07/26/2023, 1:05 PMdoNothing(because: String): Unit
with the because
parameter describing why we are doing nothing there. It gets removed on compilation but adds an opportunity to explain rather than just have something like // NO-OP
without much context regarding why there is no operation.diego-gomez-olvera
07/26/2023, 1:07 PMdiego-gomez-olvera
07/26/2023, 1:07 PMpablisco
07/26/2023, 1:10 PMdoNothing(because = "blah")
And yeah, also it can nudge the user to have to provide a description.
Also, we can quite easily search for all the usages. Handy if we want to audit that we are not getting into an unstable state due to a dead logical branch in the code.pablisco
07/26/2023, 1:12 PMdoReturn
. To provide descriptions for magic numbers that don't scream at you hehediego-gomez-olvera
07/26/2023, 1:14 PMdiego-gomez-olvera
07/26/2023, 1:15 PMdoX
functions?pablisco
07/26/2023, 1:19 PMval DOUBLE_ZERO = 0.0
This reminded me that using constants for "magic numbers" has kind of been reduced to the action and we have forgotten about why we do it. Which happens a lot on tech.
Damn, I am chatty today 🤭diego-gomez-olvera
07/26/2023, 3:55 PMusing constants for "magic numbers" has kind of been reduced to the action and we have forgotten about why we do itI guess that the naming convention comes from C macros, but this is more #random than #codingconventions not kotlin but kotlin colored
pablisco
07/26/2023, 3:56 PM