The proposed injection is readable, understandable, and easy to verify.
Resetting an object is an option if the loop is large or if MyClass
is expensive to create (for example, it opens a connection, file descriptor, subprocess, or starts a thread).
Since the reset()
solution takes more time and concentration to check with each code change, I recommend actually measuring that this is a bottleneck before worrying.
source share