kotlin?.apply{null} Anti-pattern!

TLDR

Lemme Explain.

foo?.also { //it: Foo
it.someFunc()
}
foo?.also { //it: Foo
someFunc(it)
}

The Anit-Pattern?

Paul’s Recommendation

My bite from the cake

var init:Intent? = getIntent()
init.putExtra("foo","pokemon")
init.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )
startActivity(init)
isloading = false
var init:Intent? = getIntent()
init.putExtra("foo","pokemon")
hideDialog()
init.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )
showToast()
sharedPref.update(pokemon)
SendfirebaseEvent("Exiting activity 1")
startActivity(init)
SendfirebaseEvent("Starting activity 2")

startActivity( getIntent()?.also {
it.putExtra("foo","pokemon")
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )
})

The End…or is It?

startActivity( getIntent()?.also {
it.putExtra("foo","pokemon")
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )
})

Here comes Elvis to rescue. ?: )

val length = fooArray?.length ?: -1
startActivity( getIntent()?.also {
it.putExtra("foo","pokemon")
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )
}?:Intent(this,SOME_ERROR::class))

Conclusion.

Coding enthusiast! love Android #kotlinAlltheWay, want to explore all opportunity around it! CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store