[KOTLIN] Naming Convetion

[KOTLIN] Naming Convetion

Last modified on 2025-04-18 , by hjjae2

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) #

  1. Property declarations (Initializer blocks)
  2. Secondary constructors
  3. Method declarations
  4. 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.

μš”μ•½

  1. Property (μ΄ˆκΈ°ν™” 블둝)
  2. (Secondary) μƒμ„±μž
  3. Method
  4. 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)



κ·Έ μ™Έ (Formatting, Documentation comments, Avoid redundant constructs, Idiomatic use of language features, …)