So if extension methods and extension properties are really static methods and properties. And static methods and properties and methods are not thread safe and therefore should be avoided then extension methods and extension properties are bad.
We are just tricked to do those because the codes we write will appear as pretty or clean but performance-wise it is not.
Is this true?
It depends on how you wrote extension function/property. If they doesn't edit or access shared state i.e. if property and function is clear function: it absolutely not bad practice.
Example 1:
fun String.countSpaces(): Int {
return this.count { c -> c == ' ' }
}
This functions works perfectly in multi-threaded environment, since String
is immutable.
Example 2:
data class MutablePerson(val name: String, var speech: String)
fun MutablePerson.count(nextNumber: Int) {
this.speech = "${this.speech} ${nextNumber}"
}
This function mutates speech
property of MutablePerson
object and assign operation is not atomic. If count
will be called on one object from different threads - inconsistent state possible.
Example:
fun main(args: Array<String>) {
val person = MutablePerson("Ruslan", "I'm starting count from 0 to 10:")
(1..10).forEach { it ->
Thread({
person.count(it)
println(person.speech)
}).start()
}
Thread.sleep(1000)
println(person.speech)
}
Possible output:
I'm starting count from 0 to 10: 1
I'm starting count from 0 to 10: 1 3
I'm starting count from 0 to 10: 1 3 4
I'm starting count from 0 to 10: 1 3 4 2
I'm starting count from 0 to 10: 1 3 4 2 5
I'm starting count from 0 to 10: 1 3 4 2 5 8
I'm starting count from 0 to 10: 1 3 4 2 5 6
I'm starting count from 0 to 10: 1 3 4 2 5 6 7
I'm starting count from 0 to 10: 1 3 4 2 5 6 7 9
I'm starting count from 0 to 10: 1 3 4 2 5 6 7 9 10
I'm starting count from 0 to 10: 1 3 4 2 5 6 7 9 10
So extension functions and extensions properties not bad practice, they are just like properties and method in classes: depending on how you wrote they thread safe or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With