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?

推荐答案

A good reference from Android

https://developer.android.com/kotlin/style-guide#naming_2

特殊的前缀或后缀,如示例name_中看到的那些 除非用于备份,否则不使用mName、s_name和kName 属性(请参见"Backing properties").

Kotlin相关问答推荐

判断字符串是否除了.&" ",","@""""

在Kotlin lambda的参数中如何指定函数类型?

为什么会出现Kotlin.Unit错误以及如何修复它?

为什么 trySend 会发出假数据?

使用事务时未调用 Kafka ConsumerInterceptor onCommit

Kotlin 中二叉树的深度

Kotlin:查找集合中最常见的元素

将 Gradle 子元素与 Kotlin 多平台一起使用

jetpack compose 将参数传递给 viewModel

什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

Jetpack Compose State:修改类属性

Kotlin:子构造函数如何使用其父构造函数的辅助构造函数?

创建Spring和#180的实例;Kotlin中的参数化类型引用

Gradle:无法连接到 Windows 上的 Kotlin 守护程序

如何将 CameraView 与 Jetpack Compose 一起使用?

如何在Kotlin中使用Handler和handleMessage?

Failure delivering result on activity result

尾随 lambda 语法(Kotlin)的目的是什么?

Kotlin:测试中的 java.lang.NoSuchMethodError

Kotlin中默认导入哪些包/函数?