Source code organization #
Source file names #
UpperCamelCase
Source file organization #
Placing multiple declarations (classes, top-level functions or properties) in the same Kotlin source file is encouraged as long as these declarations are closely related to each other semantically, and the file size remains reasonable (not exceeding a few hundred lines). In particular, when defining extension functions for a class which are relevant for all clients of this class, put them in the same file with the class itself. When defining extension functions that make sense only for a specific client, put them next to the code of that client. Avoid creating files just to hold all extensions of some class.
μμ½
- λͺ¨λ ν΄λμ€μμ μ¬μ©λλ ‘νμ₯ ν¨μ’ : ν΄λΉ ν΄λμ€ λ΄μ μ μΈ
- μΌλΆ ν΄λμ€μμ μ¬μ©λλ ‘νμ₯ ν¨μ’ : ν΄λΌμ΄μΈνΈ ν΄λμ€ μͺ½μ μ μΈ
- λͺ¨λ νμ₯ν¨μμ λν΄ νμΌμ μμ±νλ κ²μ νΌν κ²
Class layout (order) #
- Property declarations (Initializer blocks)
- Secondary constructors
- Method declarations
- Companion Object
Do not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods. Instead, put related stuff together, so that someone reading the class from top to bottom can follow the logic of what’s happening. Choose an order (either higher-level stuff first, or vice versa) and stick to it.
Put nested classes next to the code that uses those classes. If the classes are intended to be used externally and aren’t referenced inside the class, put them in the end, after the companion object.
μμ½
- Property (μ΄κΈ°ν λΈλ‘)
- (Secondary) μμ±μ
- Method
- Companion κ°μ²΄
- λ©μλ μμ± μ, ‘μνλ²³’, ‘μ κ·Όμ μ΄μ’ μμΌλ‘ μμ± X
- ‘κ΄κ³κ° μλ’ λ©μλ μμΌλ‘ μμ± (μμ°μ€λ½κ² λ‘μ§μ μ΄ν΄ν μ μκ²)
- ν΄λΉ ν΄λμ€λ₯Ό μ¬μ©νλ μ½λ μμ μ€μ²© ν΄λμ€ λ°°μΉ (??)
- ν΄λμ€κ° μΈλΆμμ μ¬μ©λλλ‘ μλλκ³ ν΄λμ€ λ΄λΆμμ μ°Έμ‘°λμ§ μλ κ²½μ° : Companion κ°μ²΄ λ€μ μμ±
Interface implementation layout #
When implementing an interface, keep the implementing members in the same order as members of the interface (if necessary, interspersed with additional private methods used for the implementation).
μμ½
- μΈν°νμ΄μ€, ꡬν체μ μμ± μμ : λμΌ
Overload layout #
Always put overloads next to each other in a class.
μμ½
- ‘μ€λ²λ‘λ©’μ λΆμ¬μ μμ±
Naming rules #
Package & Class #
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.
μμ½
- ν¨ν€μ§ : lowercase
- 볡μ λ¨μ΄ μ¬μ© μ : (1)λΆμ¬μ°κ±°λ, (2)lowerCamelCase
- λ¨, μ΅λν 볡μ λ¨μ΄ μ¬μ©μ νΌνμ.
- ν΄λμ€(κ°μ²΄) : UpperCamelCase
Function names #
Names of functions, properties and local variables start with a lowercase letter and use camel case and no underscores.
Exception: factory functions used to create instances of classes can have the same name as the abstract return type.
μμ½
- Function & Property & Local variable : lowerCamelCase
- λ¨, (κ°μ²΄λ₯Ό μμ±νλ)ν©ν 리 λ©μλ : κ·Έ ν΄λμ€μ λμΌν μ΄λ¦μ κ°μ§ μ μμ (??)
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.
μμ½
- (` μΌλ‘ κ°μΌ) 곡백 κ°λ₯
- underscore νμ©
Property names #
Names of constants (properties marked with const, or top-level or object val properties with no custom get function that hold deeply immutable data) should use uppercase underscore-separated (screaming snake case) names.
const val MAX_COUNT = 8
val USER_NAME_FIELD = "UserName"
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 constants, it’s OK to use either uppercase underscore-separated names (screaming snake case) (enum class Color { RED, GREEN }) or upper camel case names, depending on the usage.
μμ½
- μμ(const + val, val) : UPPER_CASE (underscore-separated)
- Mutable κ°μ²΄, μμ± : lowerCamelCase (??)
- Singleton κ°μ²΄ : UperCamelCase (??)
- ENUM
- UPPER CASE(underscore-separated)
- UPPER CAMEL CASE
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
}
μμ½
- (νλμ μ΄λ¦μΌλ‘) public API, μμ± μ¬μ© : (private) μμ± μ΄λ¦ μμ _(underscore) μ¬μ© (prefix)