Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'll admit I've never done Java programming, but (so far as I understand the term) dependency injection is something I use a lot when writing Python code. For example, if I'm writing a method to transfer a file over a particular kind of connection, I'll make the connection object a parameter rather than have the method set up the connection itself. If I'm writing a class, I might make the connection object a parameter to the constructor, or just construct it in a helper method that I can override for testing purposes.

Am I missing some magic goo that makes Python not require DI, or do I just not understand what Java programmers mean by DI?



> I'll make the connection object a parameter rather than have the method set up the connection itself

Parameter (Dependency) Injection.

> I might make the connection object a parameter to the constructor

Constructor (Dependency) Injection.

> construct it in a helper method that I can override for testing purposes.

Not DI.

http://martinfowler.com/articles/injection.html

Edit:

For languages such as Java and C#, wiring up these objects is quite a pain.

For duck typed languages such as Python and Ruby, it isn't that big of a pain. So we just do it. Just because you don't have an IoC framework doesn't mean you aren't practicing DI :)



Am I missing some magic goo that makes Python not require DI, or do I just not understand what Java programmers mean by DI?

Sometimes it's easier to assume Python-developers are blind to architectural problems and common solutions to those.

Not saying Python isn't used in large, complex problems or that all Python-code is spaghettti-code, but there are a lot of wannabe cool Python-hackers ("ninjas") out there and part of being "cool" in that sense is disregarding everything which looks even slightly like design patterns.

In those circles "Design patterns" means enterprisey, verbose code with FactoryFactories and not guidelines for how to structure your code to solve problems which have been solved before you.


"Design patterns" has become an ambiguous term.

There are a lot of design patterns for object oriented programming that were created specifically to overcome structural flaws in C++ and Java. These simply don't apply to some languages.

Dependency Injection is not one of these and actually has widespread applicability. Although the name always sounds wrong to me since it makes me think of dependency creep, one of the problems that it is intended to alleviate.

Design patterns have gained an "enterprisey" reputation because much of the time they are represented as if each one is a universal, language agnostic solution. They reek of cargo cult management.

Every language and/or ecosystem will have its own set of effective design patterns that will partially overlap with those of other ecosystems. Some communities care more about making names for the patterns, which could in itself be considered enterprisey.

Interestingly enough, the person that first told me about design patterns was a Python hacker.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: