Re:Rust – How do I create a global, mutable singleton? (2015)
by kstrauser on Wednesday July 22, 2020 @01:26PM
(#23919379)
I disagree with that in general (but certainly won't argue against certain very specific use cases). Being diligent about testing has made me loathe global objects; it's so much easier to write a test for a function that makes a database query when I can pass in a mock database object, rather than having to patch the environment to make that same function use a global mock. If you can get to the point that almost all tested functions operate only on things passed into them as arguments, some previously difficult things can become almost trivial.
I don't dislike global state because of some lofty goals I memorized back in college and have held up as supreme truth, but because practical experience tells me that it makes my life as a programmer more complicated than it has to be.
I don't dislike global state because of some lofty goals I memorized back in college and have held up as supreme truth, but because practical experience tells me that it makes my life as a programmer more complicated than it has to be.