Kotlin - coding convention - naming rules

XYMON·2023년 4월 18일
0

코틀린

목록 보기
4/7

Naming rules

Names of packages are always lowercase and do not use underscores (org.example.project).
Using multi-word names is generally discouraged, but if you do need to use multiple words, you can either just concatenate them together or use camel case(org.example.myProject).

Names of classes and objects start with an uppercase letter and use camel case

Function names

Names of functions, properties and local variables start with a lowercase letter and use camel case.

Exception: factory functions used to create instances of classes can have the same name as the abstract return

interface Foo { /*...*/ }

class FooImpl : Foo { /*...*/ }

fun Foo(): Foo { return FooImpl() }

Names for test methods

In tests (and only in tests), you can use method names with spaces enclosed in backticks.
Note that such method names are currently not supported by the Android runtime
Underscores in method names are also allowed in test code.

class MyTestCase {
     @Test fun `ensure everything works`() { /*...*/ }

     @Test fun ensureEverythingWorks_onAndroid() { /*...*/ }
}

Property names

Names of constants(marked with 'const', or top-level or object val properties with no custom get function that hold deeply immutable data) should use SCREAMING_SNAKE_CASE.

const val MAX_COUNT = 8
val USER_NAME_FIELD = "UserName"

//val with no custom get function that hold deeply immutable data
val COLORS_LIST = listOf("Red", "Green", "Blue")

//Names of top-level or object properties which hold objects 
//with behavior or mutable data should use camel case names
val mutableCollection: MutableSet<String> = HashSet()
// Names of properties holding references to singleton objects 
// can use the same naming style as object declarations
val PersonComparator: Comparator<Person> = 

For enum, SCREAMING_SNAKE and UpperCamelCase are OK.

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
}

Choose good names

The name of a class is usually a noun or a nout phrase explaining what the class is: 'List', 'PersonReader'.

The name of a method is usually a verb or a berb phrase saying what the method does: 'close', 'readPersons'.
The name should also suggest if the method is mutating the object or returning a new one. For instance, 'sort' or 'sorted'.

The name should make it clear waht the purpose of the entity is. - avoid using meaningless words(Manager, Wrapper).

When using an acronym as part of a declaration name, capitalize it if it consists of two letters (IOStream); capitalize only the first letter if it is longer (XmlFormatter, HttpInputStream

profile
염염

0개의 댓글