Kotlin - coding convention - Formatting

XYMON·2023년 4월 18일
0

코틀린

목록 보기
5/7

Formatting

Indent - no tabs, 4 spaces.
Semicolons are optional -> linebreaks are significant.

기타 설정들
prettier 설정에 참고하면 될듯.

Modifiers order

public / protected / private / internal
expect / actual
final / open / abstract / sealed / const
external
override
lateinit
tailrec
vararg
suspend
inner
enum / annotation / fun // as a modifier in `fun interface`
companion
inline / value
infix
operator
data

Annotations

Place annotations on separate lines before the declaration to which they are attached, and with the same indentation:

@Target(AnnotationTarget.PROPERTY)
annotation class JsonExclude

Annotations without arguments may be placed on the same line:

@JsonExclude @JvmField
var x: String

A single annotation without arguments may be placed on the same line as the corresponding declaration:

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

File annotations

File annotations are placed after the file comment (if any), before the package statement, and are separated from package with a blank line (to emphasize the fact that they target the file and not the package).

/** License, copyright and whatever */
@file:JvmName("FooBar")

package foo.bar

Wrap chained calls

When wrapping chained calls, put the . character or the ?. operator on the next line, with a single indent:

val anchor = owner
    ?.firstChild!!
    .siblings(forward = true)
    .dropWhile { it is PsiComment || it is PsiWhiteSpace }

Lambdas

In lambda expressions, spaces should be used around the curly braces, as well as around the arrow which separates the parameters from the body. If a call takes a single lambda, pass it outside of parentheses whenever possible.

list.filter { it > 10 }

If assigning a label for a lambda, do not put a space between the label and the opening curly brace:

fun foo() {
    ints.forEach lit@{
        // ...
    }
}

When declaring parameter names in a multiline lambda, put the names on the first line, followed by the arrow and the newline:

appendCommaSeparated(properties) { prop ->
    val propertyValue = prop.get(obj)  // ...
}

//If the parameter list is too long to fit on a line, put the arrow on a separate line:

foo {
   context: Context,
   environment: Env
   ->
   context.configureEnv(environment)
}

Common - condition, parameter, expression bodies, method calls, etc.

If possible, one line expression is good.
If too long, add a line break.

profile
염염

0개의 댓글