DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Dependency Injection Makes Your Code Worse

Dependency Injection Makes Your Code Worse

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

Daniel Alexiuc user avatar by
Daniel Alexiuc
·
Oct. 25, 09 · Opinion
Like (3)
Save
Tweet
Share
31.84K Views

Join the DZone community and get the full member experience.

Join For Free

"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.

Dependency injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrate AWS Secrets Manager in Spring Boot Application
  • 5 Steps for Getting Started in Deep Learning
  • NoSQL vs SQL: What, Where, and How
  • Getting a Private SSL Certificate Free of Cost

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: