This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

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: ida.adjivon/rtrieu/DOCS-10715-ET-standalone