
파일명(파스칼 케이스)
[property, secondary constructor, method, companion object]
함수의 순서는 위에서 아래로 읽었을 때 논리의 흐름을 따라갈 수 있는가
인터페이스 멤버와 동일한 순서 적용
const val MAX_COUNT = 8
val USER_NAME_FIELD = "UserName"
동작 또는 변경 가능한 데이터가 있는 개체를 보유하는 최상위 수준 또는 개체 속성의 이름은 Camel Case
val mutableCollection: MutableSet<String> = HashSet()
싱글톤 객체에 대한 참조를 보유하는 속성의 이름은 객체 선언과 동일한 명명 스타일을 사용할 수 있다.
val PersonComparator: Comparator<Person> = /*...*/
enum class의 속성은 Screaming Snake Case나 Upper Camel Case
if (elements != null) {
for (element in elements) {
// ...
}
}
class A(val x: Int)
fun foo(x: Int) { ... }
fun bar() {
foo(1)
}
(, [, or ], ) 뒤에 공백을 넣지 마라.. or ?.: foo.bar().filter { it > 2 }.joinToString(), foo?.bar(). 주위에 공백을 두지마라?class Map<K, V> { ... }).::: Foo::class, String::length 주위에 공백을 넣지 마라.? 앞에 공백을 넣지 마라. Null 허용 유형을 표시하는 데 사용된다: String?.다음 시나리오에서는 : 앞에 공백을 넣어라.
: 앞에 공백을 넣지 마라.abstract class Foo<out T : Any> : IFoo {
abstract fun foo(a: Int): T
}
class FooImpl : Foo() {
constructor(x: String) : this(x) { /*...*/ }
val x = object : IFoo { /*...*/ }
}
class Person(id: Int, name: String)
class Person(
id: Int,
name: String,
surname: String
) : Human(id, name) { /*...*/ }
class MyFavouriteVeryLongClassHolder :
MyLongHolder<MyFavouriteVeryLongClass>(),
SomeOtherInterface,
AndAnotherOne {
fun foo() { /*...*/ }
}
class MyFavouriteVeryLongClassHolder :
MyLongHolder<MyFavouriteVeryLongClass>(),
SomeOtherInterface,
AndAnotherOne
{
fun foo() { /*...*/ }
}
fun longMethodName(
argument: ArgumentType = defaultValue,
argument2: AnotherArgumentType,
): ReturnType {
// body
}
fun foo(): Int { // bad
return 1
}
fun foo() = 1 // good
= 기호를 넣고 식 본문을 4개의 공백으로 들여쓴다.fun f(x: String, y: String, z: String) =
veryLongFunctionCallWithManyWords(andLongParametersToo(), x, y, z)
val isEmpty: Boolean get() = size == 0
val foo: String
get() { /*...*/ }
이니셜라이저가 있는 속성의 경우 이니셜라이저가 길면 = 기호 뒤에 줄바꿈을 추가하고 이니셜라이저를 4개의 공백으로 들여쓴다.
private val defaultCharset: Charset? =
EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(file)
private fun parsePropertyValue(propName: String, token: Token) {
when (token) {
is Token.ValueToken ->
callback.visitValue(propName, token.value)
Token.LBRACE -> { // ...
}
}
}
when (foo) {
true -> bar() // good
false -> { baz() } // bad
}
= 기호 주위에 공백을 넣어라.drawSquare(
x = 10, y = 10,
width = 100, height = 100,
fill = true
)
list.filter { it > 10 }
fun foo() {
ints.forEach lit@{
// ...
}
}
appendCommaSeparated(properties) { prop ->
val propertyValue = prop.get(obj) // ...
}
매개변수 목록이 너무 길어서 한 줄에 다 들어갈 수 없다면 화살표를 별도의 줄에 넣어라.
후행 쉼표다.
class Person(
val firstName: String,
val lastName: String,
val age: Int, // trailing comma
)
후행 쉼표 사용의 여러 가지 이점.
모든 초점이 변경된 값에 맞춰지므로 버전 제어 차이점이 더욱 깔끔해짐.
요소를 쉽게 추가하고 재정렬할 수 있다.
요소를 조작하는 경우 쉼표를 추가하거나 삭제할 필요가 없다.
개체 이니셜라이저의 코드 생성을 단순화한다. 마지막 요소에는 쉼표가 있을 수도 있습니다.
후행 쉼표는 전적으로 선택 사항이다.
코드는 쉼표 없이도 계속 작동한다.
Kotlin 스타일 가이드는 선언 사이트에서 후행 쉼표 사용을 권장한다.