Hacker News new | ask | show | jobs
by jblow 4083 days ago
Yeah. I only heard about "dependency injection" a few months ago, and my reaction was that my brain just didn't get it, because, like, why are you making a huge deal about such a simple thing? If we made this much of a big deal out of every idea in programming, we would never be able to get anything done.

Since then I keep hearing about "Dependency Injection" so my impression is that it's gaining in popularity. But my kneejerk reaction is always that if someone is talking about this subject, they probably are not a very good programmer, just like if someone is talking about how important UML diagrams are. It is maybe a hasty conclusion but that is where my brain goes.

2 comments

Good use of DI can help one write a more comprehensible, lighter, more flexible system, where each class is responsible for doing just one or two reasonably-scoped tasks. Unlike, say, an obsession with UML, DI is _not_ the hallmark of a crap-grade programmer. Nor is it all that simple, in the sense that it can transform your way of thinking about runtime configuration and object lifecycle management, and promotes a more flexible mindset.

That said, I absolutely despise autowiring and annotation-based DI...

Strongly agreed especially with your last sentence.
Could you provide a concrete example of a good use of DI?
In Java-land, let's say you have a class that depends on a micro-ORM library (or just a database connection, or whatever); the micro-ORM depends on a database connection, which might be yielded by a datasource, which in turn might be wrapped by a bounded connection pool.

DI lets you push all that stuff to configuration rather than wiring it up in code; it reduces drag during development because you can always say, ah, I don't need to think about how or where the micro-ORM boots up, I can tune it later... I can even share the connection pool across five different consumers that are not aware of each other and share no compile-time dependency.

if I had knee jerk reactions, one would be that programmers who have knee jerks reactions aren't very good programmers