DEV Community

Discussion on: Not Kotdog: Using Computer Vision to Detect Hot Dogs in Kotlin

Collapse
 
devsiebe profile image
Siebe Sysmans

You're defining the ClarifaiManager as nullable aswel as making it mutable in your Activity with var manager: ClarifaiManager? = null and then initialize it in your onCreate.

Imo, there's a better way so that you don't have to mark it as nullable and have it immutable with the following code:

val manager: ClarifaiManager by lazy { ClarifaiManager(this, getString(R.string.api_id), getString(R.string.api_secret)) }

The val makes sure it's immutable, ClarifaiManager without a question mark makes sure it's never null and by lazy is gonna run the initialization code behind it the first time it's requested.

Collapse
 
adammc331 profile image
Adam McNeilly

I've made the change. Thanks again!

Collapse
 
adammc331 profile image
Adam McNeilly

Wow, that's a great recommendation, thank you so much! I didn't realize Kotlin had the lazy property until your comment. More on that here - kotlinlang.org/docs/reference/dele...

I will try to make this update later today.