DEV Community

Discussion on: What coding concept or practice seem commonly understood, but you never learned?

Collapse
 
matpk profile image
Matheus Adorni Dardenne

Dependency injection. It always rubbed me the wrong way.

Collapse
 
kachidk profile image
Nwanguma Victor

same too

Collapse
 
drmikecrowe profile image
drmikecrowe

Amen

Collapse
 
darthbob88 profile image
Raymond Price

Yeah, I understand the basic concept of passing dependencies in as a parameter, but the frameworks for doing that are always too magic for my taste.

Collapse
 
matpk profile image
Matheus Adorni Dardenne

Exactly how I feel. I know how to use it, but it feels............ wrong.

Collapse
 
marcello_h profile image
Marcelloh • Edited

it is a good way of not hard-wiring dependencies into parts of the code.
That code should work independently, which makes testing far less complex.

To explain it a bit more:
we want to travel so we take a vehicle to do that.
the vehicle should be able to steer, break and so on.
As a dependency, I give it a car to work with, and the vehicle is now a car and does satisfy its needs, but a plane, a truck, a bike etc. also can do that.
To actually test the software for "the break()" function, you can feed it a mocked-vehicle, that will help you in testing the behaviour. But you can also choose to have a "real" vehicle, up to you.

But if you wrote it by just accepting a car, then that's all it can work with.

Collapse
 
matpk profile image
Matheus Adorni Dardenne

SOLID, specifically the "L", implies that a function that requires a "vehicle" must work with cars, bikes, and planes without breaking the application. Dependency injection is not needed.