Which do you prefer? :one: ```if (isValidInput) {...
# codingconventions
a
Which do you prefer? 1️⃣
Copy code
if (isValidInput) {
    doSomething()
    doSomethingElse()
} else {
    handleInputError()
}
2️⃣
Copy code
when {
    isValidInput -> {
        doSomething()
        doSomethingElse()
    }
    else -> handleInputError()
}
2️⃣ 2
1️⃣ 11
y
Even better:
Copy code
require(isValidInput)
K 1
p
Wouldn't require throw? Is above error() function pseudo code or the actual error() function in kotlin. Not a big fan of design software based on throwing exceptions. In regards to the original question, if error() is pseudo code:
2
a
Let's say error() = handleInputError()
p
Ah ok, so is not the kotlin.error() function which basically throws an IllegalStateException(). Then
2
is clearer in my preference.
r
FWIW I increasingly find a 2 branch
when
nicer to read than an
if
.
h
I have a side note/question about the two examples above, if I may... We are trained to put single line if..else statements into curly braces, because it's "safer" in case we later need to add a statement. We don't do it with when..else though 🤔 How is
Copy code
if (..) {
    ..
} else handleInputError()
any different from
Copy code
when {
    .. -> {
        ..
    }
    else -> handleInputError()
}
in that regard? Do you guys mind single line if..else without braces?
At least for "ternary operator ifs" the braces convention seems to be relaxed in Kotlin. https://kotlinlang.org/docs/control-flow.html#if-expression still uses braces with single line if..else statements though. Is the reasoning behind it statement vs. expression form of the
if..else
?
a
I kinda use both. For pure logic, I prefer 1 only if there are just 2 choices. As it more readable (for me). When if it is an assignment (even if there is just one if-else), prefer the when-statement:
Copy code
// Just logic
if (x == 3) 
  doSomething()
else 
  doSomethingElse()

// Assignment
val r = when {
  x == 3 -> calculateSomething(x)
  else -> calculateSomethingElse(x)
}
d
Re original question: I increasingly prefer the dumbest solution appropriate to the problem. So if I have a boolean expression I tend to use "dumb" if, because when is more flexible, and using that would signal to me that I'm doing something complex. The same line of reasoning guards me against overusing/abusing the scope functions.
r
Re @horse_badorties’s question: the when version is already safe because trailing statements are never allowed in when The problem we're trying to avoid in the
if
version is if someone adds a new statement
Copy code
if (..) {
    ..
} else handleInputError()
newStatementThatShouldBeInElse()
it'll still be valid code, it just won't do what's expected But in the when version
Copy code
when {
    .. -> {
        ..
    }
    else -> handleInputError()
    newStatementThatShouldBeInElse()
}
This will be a compile error without the braces
👍🏼 1
k
In practice, I've never found the
if
-without-braces to be a real issue, despite hearing warnings about it many times. It's easy to see that an
else
part has no braces and to add them when adding a second statement. One thing I do, however, for the sake of consistency, is that if the
if
part uses braces, then I ensure the
else
part uses braces too, and vice versa.
a
A lot of this depends on the code base one is working on. Sometimes part of the standard says that if should have brackets. In such as case it would stupid to deviate from it, especially on an old code base, and/or as part of a team. Personally i tend to favor only 3 rules: 1. Consistency 2. Readability 3. Length of the text per line. I started to enable auto formatting by default. My biggest issue is that I do not know how change the formatting rules. By I rather have a readable code base which does not force me to invest in a 4K monitor just to read the code without the need scroll 2 screens horizontal.
k
The Kotlin official convention suggests:
Prefer using
when
if there are three or more options.
https://kotlinlang.org/docs/coding-conventions.html#if-versus-when
1
a
@Kai Yuan apart from the official suggestions, I sincerely wish Kotlin just had a
when
statement, and no
if
statement at all. There is something to be said for language with less syntax than more. In the same vain I find it exceedingly pleasant not to use a semicolon at all.
2