May I know any reason why SonarQube marks this as ...
# coroutines
p
May I know any reason why SonarQube marks this as a code smell:
Copy code
suspend fun transform(input: I): O {
   return withContext(defaultDispatcher) {
                  applyTransform(input)
  }
Sonar claims this: Remove this dispatcher. It is pointless when used with only suspending functions. In my library I want to make sure this transformation runs in the default dispatcher. I understand that it will be better performance if there is no context switch but then how I enforce the library users to use the right dispatcher. Other than in the documentation
z
Is applyTransform also suspending?
p
Yes correct, it is abstract, implemented in each data mapper subclass
z
I think that’s what the warning is talking about then. Suspend functions should be safe to call from any context. That means applyTransformation should make its own decision about which dispatcher it needs to run on, if any. If it does change dispatchers, then it won’t matter which dispatcher you set here.
☝️ 1
p
Ok, thanks Zach. I was thinking about it and for the sake of performance I will remove the withContext anyway. These are internal libs anyway
u
The point is, that applyTransform should switch the dispatcher if it does any heavy lifting. You do not want to care for the dispatcher at every level of abstraction. But once, where it is known what the workload is.
👍 1
👍🏻 1