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.
The current code considers any if statement followed by a newline as needing curly braces.
if (System.getenv("foo") != null)
println("ok")
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
Most helpful comment
Same here. It's a common practice in several languages.