I’ve never been a fan of dependency injection frameworks, but what really bugs me most is the use of the term “injection”. It grates on me when I hear programmers talking about using “constructor injection” or “setter injection”. Sorry, but what you’re talking about is “passing parameters”, or if you prefer “passing arguments”. It’s bad enough that we already have two terms for the things we provide to a function to vary how it behaves, why do we need a third?
Just think about it for a moment, what exactly does “injection” mean? It’s a finely-controlled way to force a substance into some other material. Does passing a parameter sound like anything out of the ordinary? No. Now, if the framework used reflection to set a private member variable because there was no natural way to do it, then I’d be happier with the term. But only for that use case. Maybe that’s how these frameworks started out, but it sure isn’t what seems to be going on these days.
So let’s be clear, designing a class that depends on another which is typed via an interface rather than a concrete class is not anything new. It’s just the age old advice about Programming to an Interface, Not an Implementation, but taken quite literally. The fact that you pass the dependent object via the constructor [1] is just Programming 101, it doesn’t need any sort of fanfare.
[1] Although you could use a setter, don’t.
I've never really been a fan of the term 'passing arguments'. Why use such a specific term when you can say 'invoke procedures', or 'process instructions'?
ReplyDeleteSeriously, we have precise terminology because sometimes we need that precision. Not always, which is the reason that we also have the general terms.
The fact that a more general term exists, isn't a valid argument against more specific terminology.
Fair argument that the term 'Injection' is logically off, but even if you don't like it, a lot of people will understand what you mean if you use it. That alone makes the terminology valuable in my opinion.
The term "injection" is intended as a metaphor, but for this to be useful if should have some relation to the real world term. But it doesn't, at least not any more. That makes it confusing at best and dangerous at worst as developers get the wrong idea about what good programming practices look like.
ReplyDeleteAs an aside "passing arguments" is not about calling a procedure per-se it's about how you vary the behaviour of that procedure. It is a specific term that explains which of the following options was taken:
- Mutating a public member variable
- Pass a value as a procedure argument
- Setting a global variable used by the procedure
- Mutating a private member by reflection
- Using inheritance and overriding a query method
- Specializing the template/generic procedure
- etc.
Injection was probably a j2ee term in the "good" old days of corporate development.
ReplyDelete