이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: kotlin-code-style/if-else-wrapping

Language: Kotlin

Severity: Notice

Category: Code Style

Description

Inconsistent formatting of our if/else statements could mislead you to their structure, especially if you have multiple nested if/else statements.

  • Use braces whenever you nest an ‘if’ statement inside another ‘if’ statement, except for if ... else if ... sequences.
  • If one of the branches of the ‘if’ statement uses braces, use braces in both branches.
  • Short, single-line branches of an ‘if’ statement should not use braces. If you must use braces, split it into several lines.

Non-Compliant Code Examples

fun foobar() {
    if (true) if (false) foo()
    if (true)
        foo()
    else
        if (false)
            bar()
    if (true) { foo() }
    if (true) {
        foo()
    } else { bar() }

    if (true) {
        foo()
    } else bar()
    if (true) foo()
    else {
        bar()
    }
}

Compliant Code Examples

fun foobar() {
    if (true) foo()
    if (true) foo() else bar()
    if (true) foo() else if (false) bar() else baz()

    if (true)
        foo()
    else if (false)
        bar()

    if (true)
        foo()
    else
        bar()

    if (true) {
        if (false) foo() else bar()
    } else {
        bar()
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security

PREVIEWING: guillaume.barrier/ERRORT-5095-general-doc-update