- Classes are more modular, as they depend only on the interface of passed-in dependencies. Class behavior can be changed by swapping out a new component.
- Testing is simplified, since stubs can be substituted for any dependency.
- It's harder to understand how a class works when reading just that class. You may have to track down its invocation to see what kind of components are passed in.
- Is DepedencyClass.expects(:new).returns(stub)) a smell, since we should have injected that stub to the class that uses it instead?
- If a class uses DI, should one only pass in already-instantiated dependencies, or is it okay to let the calling class instantiate them?
- Am I missing any pros or cons?
tuker (and others) - I'm not sure I love the idea of providing a default value for the injected dependencies.
Since the default can be overridden, you still need to check the calling code to see whether it is. As such, you haven't addressed the issue of needing to check multiple files to be sure what's happening.
I suppose I'd prefer DI with a default specified to hard-coding a dependency though.