https://kotlinlang.org logo
#announcements
Title
# announcements
m

mplacona

07/09/2017, 10:33 AM
Hey Kotlin enthusiasts.. I just wrote a little class for a binary clock I'm building, and have made the code as small as I possibly could with my limited knowledge of Kotlin. Though I think there are still some optimisations to be made. Wondering if anyone here would care to have a look... https://github.com/mplacona/Binary-Watch/blob/master/app/src/main/java/rocks/androidthings/binaryclock/MainActivity.kt#L36
v

voddan

07/09/2017, 11:03 AM
mplacona: #codereview is a specialized channel for that
L37:
object : Runnable {
can be just a lambda
L60:
when
with 2 options should really be an if, or a binary expression:
ledControl.setLed(0, 0, 1, hour in 10..19)
L71: same as L60:
ledControl.setLed(0, 1, 1, hourUnit in setOf(1, 3, 5, 7, 9))
L88: same as above:
ledControl.setLed(0, 2, 1, minute in (10..19) + (30..39) + (50..59))
Basically you abuse `when`s when they should be `if`s or plain binary expressions. Fix that and you code will shrink x3, not speaking about improved clarity
The next step would be to add extension methods like
operator fun MAX72XX.set(x: Int, y: Int, z: Int, value: Boolean) = setLed(x, y, z, value)
. Then their usage becomes
ledControl[0, 2, 1] = true
m

mplacona

07/09/2017, 7:11 PM
Thank you @voddan! Really your feedback!
v

voddan

07/09/2017, 7:17 PM
👌 Feel free to post the reworked version at #codereview. Frankly I curious how much shorter can you get the code without the `when`s and with a bit of DSL