I imagine this is asked a lot, but when are each o...
# codingconventions
h
I imagine this is asked a lot, but when are each of the following more appropriate:
Copy code
val isFoo: Boolean get() = //foo things
Copy code
fun isFoo(): Boolean = //foo things
Does the
get
imply inexpensive and static? Does the
fun
imply expensive and dynamic? I prefer to have functions only be verbs. So in practice i'll write the above always with getters, but I wonder if that's confusing or abusive. It could lead someone to constantly grab a property that has to be computed each time, e.g.
Copy code
class Matrix(private val data: List<Vector>) {
    val rows get() = data
    val cols get() = List(numCols) { j -> Vector(List(numRows) { i -> data[j][i] }) }
}
Wouldn't this imply that
Copy code
fun foo() {
    for (row in rows) {
        // things
        rows.otherThings()
    }
}
would take the same time to compute as
Copy code
fun bar() {
    for (col in cols) {
        // things
        cols.otherThings()
    }
}
for you case I would use properties syntax