supra
08/28/2023, 1:40 PMif (null == foo || foo?.token == bar.token) { doSomeThing() }
Chris Lee
08/28/2023, 1:41 PMfoo?.takeIf { it.token == bar.token }?.let { doSomething() }
Joffrey
08/28/2023, 1:42 PMfoo
. The initial code is actually very clearChris Lee
08/28/2023, 1:43 PMJoffrey
08/28/2023, 1:43 PMdoSomething
should be run if foo
is nullsupra
08/28/2023, 1:44 PMChris Lee
08/28/2023, 1:44 PMif
statements in my code, prefering when
for clarity. Something like this reads better:
when {
foo == null -> doSomething()
foo.token == bar.token -> doSomething()
}
Chris Lee
08/28/2023, 1:46 PMfoo == null
which isn’t possible given the logic of that statement.Joffrey
08/28/2023, 1:48 PM.let
if I'm making a side-effect like here. I find that if
statements are better suited in this case.Michael de Kaste
08/28/2023, 1:49 PMJoffrey
08/28/2023, 1:50 PMif (foo == null || foo.token == bar.token) {
doSomeThing()
}
Joffrey
08/28/2023, 1:51 PMsupra
08/28/2023, 1:53 PMnull == foo
is very likely to be accidentally deleted by colleagues.Joffrey
08/28/2023, 1:54 PMsupra
08/28/2023, 1:55 PMJoffrey
08/28/2023, 1:55 PMMichael de Kaste
08/28/2023, 1:56 PMMichael de Kaste
08/28/2023, 1:56 PMMichael de Kaste
08/28/2023, 2:01 PMinline fun <T> Boolean.ifTrue(block: () -> T): T? = if (this) block() else null
inline fun <T> Boolean.ifFalse(block: () -> T): T? = if (this) null else block()
inline fun <T> Boolean?.ifTrueOrNull(block: () -> T): T? = (this != false).ifTrue { block() }
inline fun <T> Boolean?.ifFalseOrNull(block: () -> T): T? = (this != true).ifTrue { block() }
so you can write something like:
foo?.token?.equals(bar.token).ifTrueOrNull{ doSomething() }
if that soothes your heartMichael de Kaste
08/28/2023, 2:03 PMif(foo?.token?.equals(bar.token) ?: true){ doSomething() }
as wellsupra
08/28/2023, 2:06 PMJoffrey
08/28/2023, 2:07 PMsupra
08/28/2023, 2:07 PMJoffrey
08/28/2023, 2:08 PMsupra
08/28/2023, 2:14 PMJoffrey
08/28/2023, 2:20 PMtoken
property of foo
is itself nullablesupra
08/28/2023, 2:28 PMtoken
is non null valueWout Werkman
08/28/2023, 3:26 PMfoo.token
won't even compile if someone removes the foo == null
check.
I think that the proposed if statement is already complete. If I were to think of a way to describe something like this in a utility method, I would make it something like:
if (foo.isNullOr { it.token == bar.token }) { doSomething() }
They both read the same: "if foo is null or it's token equals bar's token" vs your "if foo is null or *foo*'s token equals bar's token".
I do not say that this is objectively more readable at all. I just wanted to show what I personally use in case of chained calls such as application.service<UserRepository>().isNullOr { it.isInactive }
. I think it's more explicit and it's easier to evaluate while debugging.Jacob
08/28/2023, 4:35 PMif(foo?.token?.equals(bar.token) != false){ doSomething() }
seems elegant to me. It reads doSomething unless we have a clear discrepancy in the tokens between foo and barStephan Schröder
08/29/2023, 8:01 AMif (null == foo || foo.token == bar.token) { doSomeThing() }
will work.Joffrey
08/29/2023, 8:12 AMfoo
is, though. If it's a mutable property, that is not trueStephan Schröder
08/29/2023, 8:24 AM