Hacker News new | past | comments | ask | show | jobs | submit login

I'm sure my opinion is in the minority here, but all the pages and pages of blog posts that we have collectively created to rail against various design patterns seem like we're just trying so hard to justify bad programming practices.

I don't know ruby so the example given wasn't exactly clear, but it seems like you're able to (temporarily I hope) override the method Time.now to return specifically what you want. While that certainly seems reasonable for testing purposes, I would hate to have that kind of stuff happen in production code. The fact that methods can be overridden at any point in such a manner is a detriment to predictability, on the level of the longjump statement itself. It's hard for me to believe that people actually advocate this stuff as a "good thing".

Yes its true that dependency injection is a substitute for missing language features, namely modifying private data or globally scoped objects at any point. This is a good thing! Dependency injection allows you to have the same power while still giving you the predictability that truly private scope brings. If the cost of this is a few extra lines of code and some indirection in the form of interfaces, I'd say it's well worth it.




I would never use swizzling (the Objective-C equivalent) in production code, but I use it regularly in tests. I think that's the relevant distinction.


"Yes its true that dependency injection is a substitute for missing language features, namely modifying private data or globally scoped objects at any point. This is a good thing!"

The very fact that there are globally scoped objects is an abysmal failure. That there are private data whose state can change "by magic" is also a major defect.

There are much more functional ways to deal with such issues and some people are starting to see the light.

And of course watching people argue over globally modifiying what a function does or modifying by injection what is basically a global var is a bit like watching a blind and a one-eyed argue over who can see better ; )


That there are private data whose state can change "by magic" is also a major defect.

It's only a major defect if it changes "by magic" in a way that breaks things, i.e. if the magic stuff can actually affect the result of your tests.

Consider internal state like a cache - this is harmless. It might magically change, but that shouldn't affect your output.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: