Today I witnessed my first internet lynch – that’s right I saw how the masses rose to crush someone who said something they didn’t agree with…
Now that I have your attention I can explain what happened – a guy asked a very good question on StackOverflow:
Why do I need an IoC container as opposed to straightforward DI code. That’s right this guy has questioned one of the most loved design principle there is in the entire world.
Not only has he said that he fails to see the benefit of it he was afraid that if he used it at work some of his fellow developers would not understand the project’s code.
Of course because StackOverflow is a place of learning quite a few members answered this question and explained the benefits of IoC containers and why he should use them.
If you don’t know what IoC is I recommend you read Inversion of Control Containers and the Dependency Injection Pattern before you continue reading.
Update: another good post about the subject – 200 Page Manual on Inversion of Control (plus or minus 199).
And now to the Lynch part – Joel Spolsky has gone against the public opinion and answered that perhaps IoC containers should not be used:
“I believe that if you use IoC containers, your code becomes, frankly, a lot harder to read. The number of places you have to look at to figure out what the code is trying to do goes up by at least one.”
Of course there is more to his answer (and you should read it) but in a nutshell the guy that’s been writing about how to write better software (and one of the creators of StackOverflow) thinks that IoC containers are evil.
You can see from the score the answer got (-22 and counting) and from the angry comments that Joel has pissed a lot of people.
The funny thing is that at the beginning Joel’s answer was selected as the correct answer for the question but after much pressure from the community (i.e. negative score and more angry comments) the questioner has changed his mind a choose a more “popular” answer – guess what was recommended there.
What I did like is the fact that SO democracy seemed to work here. The masses has spoken!
I’m sorry to say I agree with Joel, IoC containers tend to cause less readable code and makes debugging container related problems a little hell. I do not think that like all development tools there are places that IoC containers can solve a real problem but developers should be aware of their cost.
Of course there is no right or wrong answer here – sometimes you should use IoC but don’t overcomplicate your design if you do not need to.
What do you think?
2 thoughts on “Do we need IoC containers?”
I think you and Joel are right, and frankly, I'm quite appalled at how IoC has taken the industry by storm simply because it makes configuring dependency chains simplified. It can also makes them unclear.
I think only complicated dependency structure need IoC container.
Otherwise the straightforward DI is just fine.
Everything has trade-off.