Posted on 10/7/2009 10:20:28 PM by Justin Etheredge
Software Engineering is such a nascent practice that we have very little that we can point at for facts, and instead spend much of our time listening to circumstantial evidence from developers who have tried and had success with different tools or techniques. And at the same time, since we don’t have an infinite amount of bandwidth to try everything for ourselves, we will often listen to other developers that we trust when they say that something is not good or adds needless complexity.
Now, there are some developers take the time to carefully evaluate and think about all of the tools and practices that come there way, but just like in every industry, there is a vast majority who do not put this kind of thought and work into their endeavors. This is something that is to be expected almost everywhere though, you have a small piece of the overall populace who puts in more work, has more insight, and who shares this knowledge, and they are looked at for leadership and guidance by the majority. You know the kind of people that I am talking about, the Martin Fowlers, Bob Martins, and Kent Becks of the world.
But we can’t rely on those people for all of our technology advice because as much as they produce, they still couldn’t possibly fill the void of knowledge out there. And so we read blogs (like this one!), scour the internet, and use useful sites like stack overflow.
Honestly, I probably wouldn’t think of using stack overflow for programming advice, since I mostly see it as providing answers to purely technical questions, but it appears that at least some people do use it for this purpose. Which is why I was quite intrigued when my friend John Feminella tweeted this link to a post on stack overflow where someone was asking why they needed an IoC container instead of just doing manual injection.
If you skip the responses and head right to the end you’ll see a post by the “creator” of stack overflow, Joel Spolsky. Now I realize that over the years Spolsky has devolved into nothing more than living, breathing, walking, flamebait, but so many developers still listen to what he has to say. And with good reason, he has become incredibly successful creating a software development company which hires tons of smart people and produces what many consider to be a great product. He even has the “Joel Test”, which is a twelve point list about software best practices. If we lived in a vacuum, and you didn’t have to read constantly about some crazy statement he made, then you might be surprised to hear something like this come out of his mouth:
IoC containers take a simple, elegant, and useful concept, and make it something you have to study for two days with a 200-page manual.
But unfortunately, this is exactly what I expected out of him. Anyone who has sat down with a modern IoC container for more than 5 minutes will know that this is a complete load of crap. Joel has clearly never worked with an IoC container and is instead happy to spread FUD so that he doesn’t have to bring his software engineering skills forward. Instead, he pretends to move forward, and by doing so, holds many developers back. And that is dangerous.
Thankfully, if you go up to the top of the list of answers, you will see that the number one answer is from someone that I greatly respect, Ben Scheirman. In his post he carefully explains why an IoC container is beneficial and thankfully the power of stack overflow pushed his answer to the top and Joel’s answer to the bottom.
Unfortunately though we don’t always have this kind of filter implemented for us. We have to look at a blog post or technology and make the call for ourselves whether or not we are going to agree with it. We just have to make sure that we actually think and analyze, instead of agreeing because it is easier, convenient or more comfortable for us to do so. We have to make sure that we don’t fall prey to those who pretend to move forward, and instead listen to those who are sincerely trying to find a better way.
And as a final pièce de résistance: For all of those people out there who are scared of IoC containers, check out this (via Ninject):
public class DefaultModule: NinjectModule
{
public override void Load()
{
Bind<ILoginService>().To<LoginService>();
}
}
And this:
public class DIContainer
{
private static StandardKernel kernel =
new StandardKernel(new DefaultModule());
public static T Resolve<T>()
{
return kernel.Get<T>();
}
}
And now:
ILoginService loginService = DIContainer.Resolve<ILoginService>();
See? That wasn’t so hard!