News for hackers. Stuff that matters.
Re:So Singletons are bad, then what? by shitgoose on Thursday January 30, 2014 @03:33PM (#7153179)
Fill up the cache with mocks of the objects it needs to contain. 10 objects = 10 lines of code.

If you really want to have different cache implementation for testing purpose, use C as just an instance storage, and return reference not to self but to cache interface. Put a switch inside static C.getX() that returns IX. Inside of switch statement see if System.getProperty("isTest")=true and return XSimple (which implements IX), otherwise return XReal (which also implements IX). Few lines of code, transparent and debuggable.

Re:So Singletons are bad, then what? by dllthomas on Thursday January 30, 2014 @03:56PM (#7153302)
"Fill up the cache with mocks of the objects it needs to contain. 10 objects = 10 lines of code."

But then I'm not testing the code that uses the cache, I'm testing the cache plus the code that uses the cache. Which is a valuable test but a separate one.

Regarding your proposed implementation, that sounds like basically what I proposed here:

https://news.ycombinator.com/item?id=7153126

Re:So Singletons are bad, then what? by shitgoose on Thursday January 30, 2014 @10:22PM (#7155052)
Funny, I saw your code, but didn't put your name and code together:) There are many ways to mock a singleton. I am not against mocking it, but against over-complicating. Also, I think that we are taking unit testing a bit too far in trying to decompose the app into smallest pieces. Unit tests have specific goals, like verifying correctness of calculations or performance benchmarks. If test raises a red flag, it takes a few minutes to isolate the piece of code that is at fault.