https://kotlinlang.org logo
#codereview
Title
# codereview
t

twisterrob

08/09/2018, 11:42 PM
?.
vs
if
Which one is better in the following example and why? (open to improvements)
Copy code
if (variant is TestedVariant && variant.testVariant != null) {
	renameAPK(variant.testVariant)
}
1️⃣ 1
👆 5
Copy code
(variant as? TestedVariant)?.testVariant?.let(::renameAPK)
s

Shawn

08/09/2018, 11:44 PM
oh god, the first one for sure
it reads way more straightforwardly, the second one is awkward af, regardless of how familiar you are with Kotlin
h

Hamza

08/10/2018, 4:56 AM
ALTHOUGH ALTHOUGH ALTHOUGH if you don’t want to cast juse use the
?.
t

twisterrob

08/11/2018, 8:44 AM
thanks all 👆!
@Hamza I'm not sure I get you, I need the cast as
variant: ApkVariant
doesn't have the field
testVariant
btw, after looking at this again with a fresh mind I went with:
Copy code
if (variant is TestedVariant) {
	variant.testVariant?.run(::renameAPK)
}
because it's still readable, but less repetitive than 1️⃣
6 Views