Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "m" prefix for variables in Kotlin

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?

like image 807
Hugo Passos Avatar asked Jan 02 '18 06:01

Hugo Passos


People also ask

What does the prefix m mean in a variable name?

The use of the "m" prefix is more specific that simply denoting a "member" variable: It's for "non-public, non-static field names."

What does the prefix'm'mean in Java?

'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.

What does the prefix'I'mean in a variable name?

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.

What does the m mean in public static final fields?

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.


1 Answers

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”).

like image 152
Yuri Schimke Avatar answered Sep 18 '22 07:09

Yuri Schimke