Hi, what is the convention when having a suspend and non-suspend version of the same method, which one contains the actual implementation?
For example, when having a
get()
method (non-suspend) and a
getAsync()
method (suspendable), which one should own the logic/implementation?
Option 1 (suspendable owns the logic/implementation):
Copy code
fun get(): String {
return runBlocking {
getAsync()
}
}
suspend fun getAsync(): String {
// actual getter logic
}
Option 2 (non-suspendable owns the logic/implementation):
Copy code
fun get(): String {
// actual getter logic
}
suspend fun getAsync(): String {
withContext(dispatchers.default()) {
return get()
}
}
e
elizarov
11/17/2020, 6:23 AM
When the actual logic is non-blocking, the call it by the the simple name and name the one that wraps it with runBlocking using
Blocking
suffix. For example, see `send`/`sendBlocking` pair.
elizarov
11/17/2020, 6:23 AM
Also, there is a special #C1CFAFJSK channel
elizarov
11/17/2020, 6:25 AM
It the second case, it makes sense to expose only suspending implementation. The actual logic should be better private with some kind of
Impl
suffix in its name.
a
Animesh Sahu
11/17/2020, 9:58 AM
Use
Async
suffix when the function is not marked with suspend but returns a Deferred<T> from
CoroutineScope.async
.
Use
Blocking
suffix when it wrap around a blocking code and is non-suspendable.
Use no suffix/prefix when function is