Ktlint: experimental:multiline-if-else treats all if statements as multiline

Created on 13 Jul 2020  Â·  4Comments  Â·  Source: pinterest/ktlint

Expected Behavior

MultiLineIfElseRule.kt respects its own JavaDoc, i.e. quoting Kotlin Coding Conventions:

If the condition of an if or when statement is multiline, always use curly braces around the body of the statement.

Observed Behavior

The current code considers any if statement followed by a newline as needing curly braces.

Steps to Reproduce

        if (System.getenv("foo") != null)
            println("ok")

Your Environment

  • Version of ktlint used: 0.37.2
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): Gradle plugin
  • Version of Gradle used (if applicable): 6.5.1
  • Operating System and version: Linux 5.4.0-40-generic amd64
  • Link to your project (if it's a public repository):
bug question

Most helpful comment

Honestly, I'm in favour of Android styleguide in this case, not sure why kotlin's one does not clearly define anything here

Same here. It's a common practice in several languages.

All 4 comments

We shouldn't change this when the --android option is passed though, since it is marked as wrong there. Honestly, I'm in favour of Android styleguide in this case, not sure why kotlin's one does not clearly define anything here

Honestly, I'm in favour of Android styleguide in this case, not sure why kotlin's one does not clearly define anything here

Same here. It's a common practice in several languages.

It's a common practice in several languages.

In languages that have C-like syntax. Kotlin has an option to write code in a more functional style.

I don't want to explain Kotlin team decisions as I don't have any knowledge about their reasoning, I may only guess. Take a function:

fun createFoo(arg: Int) =
    "a".repeat(arg)

It's short, consistent and does not use any braces. Now let's evolve this function by making its behaviour dependent on the argument value:

fun createFoo(arg: Int) =
    if (arg < 5)
        throw IllegalArgumentException("Too long to keep the whole method body in 1 line")
    else
        "a".repeat(arg)

One can argue that adding braces to the if statement would make this simple function no longer consistent, no longer functional style but rather having an imperative smell:

fun createFoo(arg: Int) =
    if (arg < 5) {
        throw IllegalArgumentException("Too long to keep whole method body in 1 line")
    } else {
        "a".repeat(arg)
    }

Maybe yes, maybe no – I'm not a functional purist. One can also argue that it's weird to have braces in a function which body does not have braces - there is no closing brace on "level 0". For people used to Java braces style it looks like the code should not compile because there is a closing brace missing.

Created an issue to get JB's opinion on that (hopefully with the styleguide update) https://youtrack.jetbrains.com/issue/KT-41070

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tapchicoma picture Tapchicoma  Â·  3Comments

tonilopezmr picture tonilopezmr  Â·  4Comments

Tapchicoma picture Tapchicoma  Â·  3Comments

plastiv picture plastiv  Â·  4Comments

impatient picture impatient  Â·  5Comments