- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 144字
- 2021-06-24 19:15:29
Low coupling
The code dependency between threads is referred to as coupling. We should try to keep coupling as low as possible, to avoid complexity and make the code base easy to read and maintain. Now, what does that actually mean? Refer to the program with threading where we accessed and modified the someData value from two threads simultaneously. That can be called coupling, as both the threads were dependent on each other. For your reference, we copied the following code-snippet:
async(CommonPool) { for(i in 11..20) { myData.someData+=i println("someData from 1st async ${myData.someData}") delay(500) } } async(CommonPool) { for(i in 1..10) { myData.someData++ println("someData from 2nd async ${myData.someData}") delay(300) } }
In the next program, where we introduced immutability, the coupling is reduced. Here, both the threads were reading the same element, but one thread's operations and changes didn't affect the other one.