Zakhar
10/22/2023, 2:46 PMCoroutineScope(Dispatchers.Default).launch { updateRooms() }
private suspend fun updateRooms() {
mutex.withLock { for (room in rooms) room.update() }
delay(delay)
updateRooms()
}
or this:
CoroutineScope(Dispatchers.Default).launch {
while (true) {
mutex.withLock { for (room in rooms) room.update() }
delay(delay)
}
}
The first one uses a recursion. Should i care about stack overflow in this case?Sam
10/22/2023, 2:53 PMtailrec
to make the first one work without stack overflowJoffrey
10/22/2023, 2:55 PMtailrec
as Sam said, but that's not the most important. You should rather care about the readability of your code. Using recursion here is weird, because the behaviour you want for your system is an infinite loop by definition.
Some problems are inherently recursive, like divide-and-conquer algorithms, and using recursion in the code to solve those problems makes sense because it aligns with how you think about them. You usually start with the recursive code because it makes sense and is readable, and then maybe you switch to iterative code (if possible) when/if you need to improve the memory footprint at the expense of readability.
But that is not your case here. You don't have a recursive problem. You have a loop, and you choose to express it recursively for no apparent reason. It doesn't match how you think about the problem, so it's not nice to read it this way.Chris Fillmore
10/22/2023, 4:47 PMwhile (isActive)
is suitable hereJoffrey
10/22/2023, 4:55 PMChris Fillmore
10/22/2023, 6:23 PMVishnu Shrikar
10/23/2023, 12:12 AMRonny Bräunlich
10/23/2023, 4:53 AM