Hi all :android-wave: , What is the recommendation...
# detekt
a
Hi all 👋 , What is the recommendation for code like this? Code
Copy code
override fun setAlarm() {
        CoroutineScope(
            context = dispatcherProvider.io,
        ).launch {
            // ...
            val alarmManager = getAlarmManager() ?: return@launch
            // ...
        }
}
Error
Copy code
Expression with labels increase complexity and affect maintainability. [LabeledExpression]
The idea is to return early if required, from inside the code blocks.
b
if/else, for example. Move all the code inside the
launch
to another function so you just
return
inside a regular function, etc. Any way that avoids the usage of labeled expressions. With that code I need to go up and read again to see what
@launch
is. And what does is implies.
🆗 1
e
unrelated but creating a disconnected CoroutineScope like that is bad practice
a
Hi @ephemient, This method is called from a
BroadcastReceiver()
. Any suggestion for this?
creating a disconnected CoroutineScope like that is bad practice
e
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/
taking care to cancel these coroutine scopes when they are no longer needed (see section on custom usage below for explanation and example)
you should probably start with a
MainScope()
that is set up to be cancelled when your receiver is destroyed or similar
a
Okay, I will explore more about that. thank you color