`?.` vs `if` Which one is better in the following ...
# codereview
t
?.
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
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
ALTHOUGH ALTHOUGH ALTHOUGH if you don’t want to cast juse use the
?.
t
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️⃣