https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Conor Fennell

03/11/2021, 5:30 PM
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

wbertan

03/11/2021, 5:44 PM
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

elizarov

03/12/2021, 6:01 AM
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
2 Views