Florian
08/22/2021, 12:04 PMYoussef Shoaib [MOD]
08/22/2021, 12:42 PMvar
instead of val
for shadowing, but it still should apply. For reference, https://discuss.kotlinlang.org/t/function-parameters-are-val-not-var/897/5?u=kyay10 and https://stackoverflow.com/questions/44109098/how-do-i-make-method-param-mutable-in-kotlinFlorian
08/22/2021, 1:09 PMJakob Löhnertz
08/22/2021, 1:33 PMfiniteProgress
for the variable ofc, but well, it's quite clunky.
What I would personally go for is that the either the parameter and/or the variable contains the unit (probably better the parameter, such that it is clear for the callers); is this a percentage, or some other scale? And then you can keep the variable name as is, because in the context of the function, it's probably quite clear.edrd
08/22/2021, 1:42 PMFloat
you would have a Progress
type:
@JvmInline
value class Progress(private val value: Float) {
fun toFloat(): Float = if (value.isNaN()) 0f else value
}
(note: although there's the toFloat
method, you should pass Progress
around and only use it if necessary -- e.g. when calling libraries)
This is nice because:
1. It moves the normalization logic out of your function, reducing complexity and making it reusable.
2. You can test it separately.
3. You can create extension functions for it without polluting Float
namespace.
4. It has no overhead because of value classes (❤️)
More on this here: https://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling/Florian
08/22/2021, 3:44 PM