```class IntentOpener(private val activity: Activi...
# getting-started
u
Copy code
class IntentOpener(private val activity: Activity?) {

    private fun startActivity(intent: Intent): Boolean {
        val activity = activity ?: return false <-------------------
        activity.whatever()
        return true
    }
}
vs
Copy code
class IntentOpener(private val activity: Activity?) {

    private fun startActivity(intent: Intent): Boolean {
        if (activity == null) return false <-------------------
        activity.whatever()
        return true
    }
}
Which variant is more idiomatic? First one shadows but doesn't rely on smart casts
y
2nd one is more idiomatic since it avoids the unnecessary shadowing. You should definitely not be scared of smart casts! This'd be better, but it depends on what
whatever
returns:
Copy code
private fun startActivity(intent: Intent): Boolean {
        activity?.whatever() ?: return false
        return true
    }
u
Btw Is shadowing an issue? seems to compile fine and works correctly
y
Not an issue. I just wouldn't use it if it's not needed. If
activity
was a
var
, then sure (although my variant still works then)
➕ 1
u
I see thank you!