Howdy, What would be the idiomatic Kotlin way to ...
# getting-started
c
Howdy, What would be the idiomatic Kotlin way to go here?
Copy code
data class Calculation(
    val money: Double,
    val multiplier: Double,
)
fun Calculation.total(): Double = money * multiplier
OR
Copy code
data class Calculation(
    val money: Double,
    val multiplier: Double,
) {
    fun total(): Double = money * multiplier
}
w
I would do like this:
Copy code
data class Calculation(
        val money: Double,
        val multiplier: Double,
    ) {
        val total: Double
            get() = money * multiplier
    }
2
But about the question of making extension or not. I would see, if I would use it everywhere
Calculation
is required, I would put inside
Calculation
. If it would be in only one specific module, I would make an Extension with visibility only in that module.
👍 5
e
I would look at whether the
total
is conceptually a part of
Caculation
or just an utility. See https://elizarov.medium.com/extension-oriented-design-13f4f27deaee
👍 5