Dependency Injection Makes Your Code Worse
Injection" - you've probably heard and uttered this phrase so many
times that it has become almost meaningless now. But just humour me for
a moment. Say it slowly to yourself and imagine you are hearing this
phrase for the first time.
It sounds insidious doesn't it?
If you went to the doctor and he
told you he was going to give you a "dependency injection", then you
would probably think there was something seriously wrong. If you were
to buy a house, and the seller told you he wanted to inject a few more
dependencies into the contract - would that sound good to you?
There is not much to like about either of those two words: "dependency" and "injection".
And it is the same thing with programming. Dependencies are not good, they are bad! They should be things that we try to avoid wherever possible. We spout principles like "loose coupling" and "high cohesion" to make people think that our code is structured and organised. But far from improving our code by being a way to structure and organise it, "dependency injection" actually makes it worse.
Questions worth considering
How can you possibly reason
logically about what a method or class does when dependencies are being
squirted in all over the place?
How could a system that facilitates the adding of dependencies to your classes possibly be beneficial? Especially when those dependencies are being managed by a "dependency injection framework" and you don't even know for sure what those dependencies will be?
A symptom of a larger problem
Most Java code I have seen (and written myself) is a gallimaufry of frameworks, patterns and objects that obscure meaningful code.
Dependency injection may have
been developed as an attempt to solve some of these problems, but
fundamentally it is adding to the problems. It is yet another framework
to add to the tangled mess of code that allows you to abstract any real
meaning further away and which enables your methods and classes to
behave in more and more unpredictable ways.
My real gripe, though, is not with Dependency Injection. Dependency Injection is just a symptom (albeit a very obvious one) of the real problem facing Java programmers: the object-oriented paradigm.
Deep down you know itThere are other obvious symptoms that we are doing things the wrong way. Can you put your hand on your heart and honestly say that you are happy and productive with Dependency Management frameworks like Maven, and ORM frameworks like Hibernate? Or are they something that you spend way too many billable hours fiddling with?
The object-oriented paradigm has encouraged a generation of programmers (myself included) to write code in the most abstract, meaningless and side-effecting way possible. And even though we don't really use Java in an object-oriented way anymore (because deep down we know it just doesn't fundamentally make any sense), that same paradigm has taught us to write code that cannot be relied upon.
It cannot be relied upon because data is being twisted and untwisted into objects, those objects are being bombarded with dependencies, and those dependencies are recursively being infected by other dependencies.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)