扩展方法和扩展属性是不好的做法吗?

LEM*_*ANE 6 kotlin kotlin-extension

因此,如果扩展方法和扩展属性确实是静态方法和属性.静态方法,属性和方法不是线程安全的,因此应该避免扩展方法和扩展属性不好.

我们只是被欺骗了,因为我们编写的代码看起来很漂亮或干净,但性能方面则不然.

这是真的?

Rus*_*lan 9

这取决于你如何编写扩展函数/属性.如果他们不编辑或访问共享状态,即如果属性和功能是明确的功能:它绝对不是不好的做法.

例1:

fun String.countSpaces(): Int {
    return this.count { c -> c == ' ' }
}
Run Code Online (Sandbox Code Playgroud)

这个函数在多线程环境中完美运行,因为它String是不可变的.

例2:

data class MutablePerson(val name: String, var speech: String)

fun MutablePerson.count(nextNumber: Int) {
    this.speech = "${this.speech} ${nextNumber}"
}
Run Code Online (Sandbox Code Playgroud)

该函数改变speechMutablePerson对象的属性,并且赋值操作不是原子的.如果count将从不同的线程调用一个对象 - 可能的不一致状态.

例:

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)
}
Run Code Online (Sandbox Code Playgroud)

可能的输出:

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
Run Code Online (Sandbox Code Playgroud)

所以扩展函数和扩展属性也不错,它们就像类中的属性和方法一样:取决于你如何编写它们的线程安全与否.


Oll*_*liP 7

静态方法只有实例方法才有自己的堆栈.因此静态方法中的临时变量就像例如方法一样在栈上.在访问共享状态时,传递给静态方法的参数可能会遇到线程问题,但这与实例方法完全相同.

考虑Java中的大量Util类和静态方法,作为Java没有扩展功能的解决方法.关于多线程,没有任何问题.

同样在C#扩展方法中是场景静态方法的后面,它没有任何损害,请参阅如何在内部实现扩展方法