Using “m” prefix for variables in Kotlin

后端 未结 3 1375
日久生厌
日久生厌 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()
    
        val elementList: List
             get() = _elementList
    }
    

提交回复
热议问题