-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
experimental:multiline-if-else treats all if statements as multiline #812
Comments
We shouldn't change this when the |
Same here. 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 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 |
My 2 cents is that I don't think this is something that needs to be strictly linted. I think the main benefit of using Ktlint is to get rid of formatting changes happening back and forth in PRs which just contribute noise. In this case there's not gonna be any auto formatting, and it can be left to the programmer to decide what best fits the use case. So my opinion is that all can be considered correct.
|
The new
|
Enforces that single line if-statements are kept simple. A single line if-statement is allowed only when it has at most one else branch. Also, the branches of such an if statement may not be wrapped in a block. Closes #812 Add discouraged comment locations to keep rule simpler.
* Add rule `if-else-wrapping` Enforces that single line if-statements are kept simple. A single line if-statement is allowed only when it has at most one else branch. Also, the branches of such an if statement may not be wrapped in a block. Closes #812 * Add discouraged comment locations to keep rule simpler.
@paul-dingemans will this still be allowed? if (condition) 1
else 0 |
No, it is a multiline statement. But |
So how does one correctly format this code from above?
or worse:
|
It depends on your configuration. But running Ktlint CLI ( Given
the code should be formatted as:
(I have wrapped the second example into function |
I did not know the -F flag! THANK YOU for replying on this old thread. Addressing this scenario put me at the peak of frustration. |
Expected Behavior
MultiLineIfElseRule.kt respects its own JavaDoc, i.e. quoting Kotlin Coding Conventions:
Observed Behavior
The current code considers any
if
statement followed by a newline as needing curly braces.Steps to Reproduce
Your Environment
The text was updated successfully, but these errors were encountered: