The benefits of dependency injection are:
- Separating configuration of dependencies from their use.
- Improving unit testability.
- Making dependencies explicit.
As I finished explaining these points to some new hires, I followed it up with a generalisation, "And in general, being explicit is better than being implicit - it reduces ambiguity and improves communication, always welcome in software development." Immediately I was struck by a few incongruities. After all, we have come to appreciate:
- The absence of Java-esque checked exceptions in C#.
- The flexibility afforded by dynamically typed languages.
- The evolution friendliness and interoperability of loosely typed SOAP web service contracts (documents, coarse-grained interfaces, restricting ourselves to the simplest of XSD types).
In the above cases, being explicit comes with a stiff cost. We trade it off for flexibility. So the generalisation still holds. In general, being explicit is better than being implicit. But there is a caveat implicit in there - "In general, being explicit is better than being implicit provided everything else remains constant." If there are additional benefits to be gained from going implicit, then being explicit is questionable.
It helps to make this caveat explicit. A case of making the generalisation practise what it preached.
It helps to make this caveat explicit. A case of making the generalisation practise what it preached.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.