Using “m” prefix for variables in Kotlin

后端 未结 3 1370
日久生厌
日久生厌 2021-02-07 14:05

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 va

相关标签:
3条回答
  • 2021-02-07 14:26

    I actually don’t think it’s good practise to have prefixed variables in the public API, thus foo.mName = "Foo" would be undesirable. For private fields this would be acceptable though.

    The official conventions for the Kotlin language say:

    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
    }
    
    0 讨论(0)
  • 2021-02-07 14:34

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

    Therefore you should not use the "m" prefix for variables in Kotlin.

    0 讨论(0)
  • 2021-02-07 14:41

    A good reference from Android

    https://android.github.io/kotlin-guides/style.html

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

    0 讨论(0)
提交回复
热议问题