Code review horror in 4 lines of code
published: December 30, 2021 |
updated: February 11, 2022
I run into the following code during code review and had an immediate and visceral reaction.
This is a (bad) attempt to add thread safety, because you are getting a value through a read only interface, but there is still the mutable instance to work with at the source, and now you have someone that observes the instance while it is being mutated, outside the lock.
The proper way to handle this is to copy the list (under the lock) and return a distinct copy.
Woah, already finished? 🤯
If you found the article interesting, don’t miss a chance to try our database solution – totally for free!