Derik Whittaker

Syndication

News


IoC/DI should be about creating Loosely coupled code, not testable code

The other day I was watching a video about IoC/DI from Google and about 47 seconds into the video I got a bit upset.  Why did I get a bit upset?  Because off the bat he talks about IoC/DI as being a concept to simplify testing and creating testable code.  I am sorry but this is NOT the goal of IoC/DI, but rather a nice to side affect but OMG, it should NEVER be the goal.

In my opinion DI/IoC concepts are designed to give you  loosely coupled code, code that has very limited knowledge of its dependencies and code which can be refactored/changed easily. 

Now do not get me wrong, if you do follow DI (or any of the SOLID principles for that fact) you WILL indeed have code which is more testable than if you did not follow these principles.  But to me the fact your code is more testable is a nice side affect of having good, clean code.

So, just to recap….

When you follow DI (or SOLID) principles you are creating code which should become simpler to maintain.  This in turn should provide you code which is simpler to tests.  It is NOT the other way around.

Till next time,


Posted 05-22-2009 6:24 AM by Derik Whittaker

[Advertisement]

Comments

Kevin Berridge wrote re: IoC/DI should be about creating Loosely coupled code, not testable code
on 05-22-2009 10:33 AM

I agree with you that IOC/DI are all about loose coupling.  I didn't watch the video, but I don't think that's why people usually start using it.

People who have followed the solid principles have seen that loose coupling does indeed make your life in code much easier, both at the time you're writing and even more so when it comes time to maintain it later.  So loose coupling is a good thing, but its rarely a goal all its own.

TDD on the other frequently is a goal.  And TDD is virtually impossible without introducing DI or IOC.  And I think that's why you hear the IOC/DI being discussed as a tool to help with unit testing so often.

Fortunately, both view points lead to the same end!

Nik Radford wrote re: IoC/DI should be about creating Loosely coupled code, not testable code
on 05-22-2009 10:53 AM

Side effect* not affect.

Effect of something.

You affect something.

:)

Nigel Spencer wrote re: IoC/DI should be about creating Loosely coupled code, not testable code
on 05-22-2009 1:48 PM

@Kevin - Whilst I think that both IOC/DI and TDD can both be useful practices in writing code I'm a bit confused when you say that TDD is "frequently a goal". How is TDD a goal? Surely the goal is likely to be high quality software, TDD (with or without IOC/DI) is just one method of achieving that goal. Maybe I've missed your point - but I would have said the goal of TDD is more maintainable software, but not that TDD itself is a goal - any more than IOC/DI is.

Sean Blakemore wrote re: IoC/DI should be about creating Loosely coupled code, not testable code
on 05-23-2009 4:19 AM

For me it depends why you decide to use an IoC tool in the first place, are you writing tests before of after implementation.

I think if you're writing unit tests after the code so that you can be confident about not introducing regressions then yes, the goal of the IoC tool being there is loosely coupled code and testability is a happy side effect.

However if you're doing TDD and writing tests, before you implement the code, as a means of software design, perhaps there is a reason to suggest that the goal of using an Ioc tool in this scenario is to increase testability. TDD as a form of sotware design functions on the premise that by writing tests first, you will likely have an end product which is testable, loosely coupled and well designed. So if you introduced an IoC tool to facilitate more productive TDD, surely the goal is making it easier to test?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)