在 Kotlin 中为变量使用 "m" 前缀
Using "m" prefix for variables in Kotlin
使用 "m" 前缀作为变量名在编程中变得很常见,主要是在 Android 中,但是自从 Kotlin 出现后,这个小问题让我有点困扰。
设置和获取带有 "m" 前缀的变量看起来不太好,因为在 Java 中我们创建(并命名)我们的 setter 和 getter,所以我们可以省略 "m",但这在 Kotlin 中不会发生,除非我们反其道而行之并重复 Java 的技术。
Java:
public class Foo {
private String mName;
public void setName(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
public class Main {
public static void main(String[] args) {
Foo foo = new Foo();
foo.setName("Foo");
}
}
科特林:
data class Foo(val mName: String)
fun main(args: Array<String>) {
val foo = Foo()
foo.mName = "Foo" // "m" prefix doesn't fit
}
我们该怎么办?是否有新的约定可循?
来自 Android
的很好的参考
https://developer.android.com/kotlin/style-guide#naming_2
Special prefixes or suffixes, like those seen in the examples name_,
mName, s_name, and kName, are not used except in the case of backing
properties (see “Backing properties”).
根据 Android Kotlin Style Guide:
Special prefixes or suffixes, like those seen in the examples name_
, mName
, s_name
, and kName
, are not used except in the case of backing properties (see “Backing properties”).
因此,您不应在 Kotlin 中为变量使用 "m" 前缀。
我实际上不认为在 public API 中使用前缀变量是好的做法,因此 foo.mName = "Foo"
是不可取的。对于 private 字段,这是可以接受的。
Kotlin 语言的官方 conventions 说:
Names for backing properties
If a class has two properties which are conceptually the same but one is part of a public API and another is an implementation detail, use an underscore as the prefix for the name of the private property:
class C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element>
get() = _elementList
}
使用 "m" 前缀作为变量名在编程中变得很常见,主要是在 Android 中,但是自从 Kotlin 出现后,这个小问题让我有点困扰。
设置和获取带有 "m" 前缀的变量看起来不太好,因为在 Java 中我们创建(并命名)我们的 setter 和 getter,所以我们可以省略 "m",但这在 Kotlin 中不会发生,除非我们反其道而行之并重复 Java 的技术。
Java:
public class Foo {
private String mName;
public void setName(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
public class Main {
public static void main(String[] args) {
Foo foo = new Foo();
foo.setName("Foo");
}
}
科特林:
data class Foo(val mName: String)
fun main(args: Array<String>) {
val foo = Foo()
foo.mName = "Foo" // "m" prefix doesn't fit
}
我们该怎么办?是否有新的约定可循?
来自 Android
的很好的参考https://developer.android.com/kotlin/style-guide#naming_2
Special prefixes or suffixes, like those seen in the examples name_, mName, s_name, and kName, are not used except in the case of backing properties (see “Backing properties”).
根据 Android Kotlin Style Guide:
Special prefixes or suffixes, like those seen in the examples
name_
,mName
,s_name
, andkName
, are not used except in the case of backing properties (see “Backing properties”).
因此,您不应在 Kotlin 中为变量使用 "m" 前缀。
我实际上不认为在 public API 中使用前缀变量是好的做法,因此 foo.mName = "Foo"
是不可取的。对于 private 字段,这是可以接受的。
Kotlin 语言的官方 conventions 说:
Names for backing properties
If a class has two properties which are conceptually the same but one is part of a public API and another is an implementation detail, use an underscore as the prefix for the name of the private property:
class C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element>
get() = _elementList
}