William Reed
10/23/2024, 1:14 PMerror()
over throw IllegalStateException
? is error
always more idiomatic?Joffrey
10/23/2024, 1:15 PMerror()
all the time. It's more idiomatic and more concise. The only reason to use throw IllegalStateException(...)
explicitly is if you want to give it a cause, which error()
doesn't supportephemient
10/23/2024, 3:04 PMcheck(x) { msg }
instead of if (!x) error(msg)
and checkNotNull(x) { msg }
instead of x ?: error(msg)
ephemient
10/23/2024, 3:05 PMIllegalArgumentException
ephemient
10/23/2024, 3:05 PMerror
or `throw IAE`/`ISE`Joffrey
10/23/2024, 4:01 PMandI agree with everything except this. When I have a chain of operations on a collection, or a sequence of function calls etc, I find it much more readable to use an elvis at the end rather than wrapping in `checkNotNull`:instead ofcheckNotNull(x) { msg }
x ?: error(msg)
list.map { ... }.firstOrNull { ... } ?: error("...")
foo.bar?.baz()?.stuff() ?: error("...")
ephemient
10/23/2024, 4:05 PMval result = list.map { ... }.firstOrNull { ... }
return checkNotNull(result) { "..." }
or similar. whether you use checkNotNull
or ?:
it doesn't really chain wellJoffrey
10/23/2024, 10:15 PM?:
is not .
.ephemient
10/23/2024, 10:18 PMcheckNotNull(...).foo
(... ?: error()).foo
is awkward either way, if ...
is a multi-line expressionJoffrey
10/23/2024, 10:19 PMerror
come last thanks to safe calls. I never nest expressions like (... ?: error())
in a chain. If I need this kind of expressions I extract them to functionsStephan Schröder
10/25/2024, 8:27 AM...
is a multi-line expression, I tend to use
...?.foo ?: error("abc")
but of course you could do:
val f = ...?.foo
checkNotNull(f)
ephemient
10/25/2024, 9:19 AMcheckNotNull(x.nullable1()).nullable2()
!=
x.nullable1()?.nullable2() ?: error()
Stephan Schröder
10/25/2024, 9:27 AMf
could be aquired. which is almost always what I want.Joffrey
10/25/2024, 9:50 AMsometimes that doesn't have the same resultRight, but in the case you describe, I'd probably extract the first part to a separate non-nullable extension.
Elena van Engelen
10/28/2024, 11:10 AMJoffrey
10/28/2024, 11:11 AMIllegalStateException
here, and we compared it with error()
, which also throws IllegalStateException
, so all good 😉Elena van Engelen
10/28/2024, 11:32 AMElena van Engelen
10/28/2024, 11:45 AMfun processPaymentMethod(paymentMethod: String) {
when (paymentMethod) {
"CreditCard" -> println("Processing payment with Credit Card")
"PayPal" -> println("Processing payment with PayPal")
else -> error("Unsupported payment method: $paymentMethod")
}
}
Joffrey
10/28/2024, 11:45 AMerror
supports lambdas, it doesn't really make sense since we're using the message 100% of the timeJoffrey
10/28/2024, 11:46 AMElena van Engelen
10/28/2024, 11:47 AMJoffrey
10/28/2024, 11:48 AMI think check/require functions are cleaner for checking conditions than doing conditions manually and then calling error()Yeah that's what we were trying to refine in the discussion above. Which cases more specifically are more suited for one or the other. As I mentioned, I like how the elvis flows with
error()
in some chained cases. For single-condition checks, the dedicated `check`/`require` functions are more appropriate indeed.Elena van Engelen
10/28/2024, 11:54 AMElena van Engelen
10/28/2024, 11:59 AM