Platinum Partner
java,java dependency injection opinion object

Dependency Injection Makes Your Code Worse

Dependencies are not good, they are bad! They should be things that we try to avoid wherever possible.

"Dependency 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 it

There are other obvious symptoms thatwe are doing things the wrong way. Can you put your hand on your heartand honestly say that you are happy and productive with DependencyManagement frameworks like Maven, and ORM frameworks like Hibernate? Orare they something that you spend way too many billable hours fiddlingwith?


The object-oriented paradigm has encouraged a generation ofprogrammers (myself included) to write code in the most abstract,meaningless and side-effecting way possible. And even though we don'treally use Java in an object-oriented way anymore (because deep down weknow it just doesn't fundamentally make any sense), that same paradigmhas taught us to write code that cannot be relied upon.


It cannot be relied upon because data is being twisted anduntwisted into objects, those objects are being bombarded withdependencies, and those dependencies are recursively being infected byother dependencies.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}