Using "m" prefix for variable names became usual in programming, mainly in Android, but since Kotlin arrived, this minor thing bothers me a bit.
Setting and getting variables with "m" prefix doesn't seem really nice, because in Java we create (and name) our setters and getters, so we can omit the "m", but this doesn't happen in Kotlin, unless we walk in the opposite of conventions and repeat Java's technique.
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");
}
}
Kotlin:
data class Foo(val mName: String)
fun main(args: Array<String>) {
val foo = Foo()
foo.mName = "Foo" // "m" prefix doesn't fit
}
What should we do? Is there a new convention to follow?
The use of the "m" prefix is more specific that simply denoting a "member" variable: It's for "non-public, non-static field names."
'm' means member of the class. As already answered this prefix indcates that a variable is member. 'm' means the variable is a member variable of the class... not only in java, I've seen similar convention in cocos2d+box2d samples where some of the variables start with m_, but others don't, very confusing.
As already answered this prefix indcates that a variable is member. Somtimes people use other prefixes if you discover some variables starting with 'i' or 's' it could also be a variant of the Hungarian Notation Show activity on this post.
Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES. Note that this is for writing Android source code. For creating Android apps, the Google Java Style Guide may be more helpful. Show activity on this post. The m is here to indicate a m ember variable.
A good reference from 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”).
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