Mike is currently…

getting whipped at Halo

Pragmatic Version Control Using Subversion

[ subversion book ]

obligatory book plug

[ syndicate ]

rss 2.0 feed for boy meets world

[ contact ]

drop me a line

[ about ]

this is mike mason's weblog

[ eskimoman.net ]

original web pages


Wed, 07 Apr 2004

This is an archived blog post. I've switched to using WordPress as my blogging software and have not migrated all my old posts. I believe strongly in not letting an old link die, so this page continues to work. Please do visit mikemason.ca/blog to read newer posts.

IoC Taken Too Far

Readers may be familiar with Inversion of Control (IoC), or as the technique is now known, Dependency Injection. Basically instead of a class allocating resources (the things it depends on) when it’s constructed, all its dependencies are created by an external “assembler” and passed to it. This helps a lot when creating testable code, because it’s easy to isolate and test single classes — simply pass in a bunch of mock or stub objects when you create the object under test.

As with any pattern, good taste and judgement are required for it to be effective. I came across this example today:

     * A Log4J logger instance - initialised using IoC rather than 
     * static logging to cut down on the number of Logger instances
    private final Logger logger;

Log4j logger instances obtained through getLogger() are just references to a singleton managed by Log4j, so passing a reference to a logger to multiple classes doesn’t actually cut down on the number of instances. This mis-application of IoC has simply increased code complexity, without providing any actual benefit.

As with any pattern or technique — including “Agile”, XP, TDD and Unit Testing — IoC or Dependency Injection should only be used when there’s a real benefit. As with much in life, there are no hard and fast rules about programming, otherwise you wouldn’t need something as complicated as a human being to write software.

Posted 22:22, 07 Apr 2004.  

Mon Tue Wed Thu Fri Sat Sun

[ tim bacon ]

musings of an xp coach

[ ian bourke ]

enhancing core competencies since 1976

[ martin fowler ]

a cross between a blog and a wiki

[ alan francis ]

agile != good

[ paul hammant ]

part of the problem…

[ darren hobbs ]

the blog formerly known as pushing the envelope

[ mike roberts ]

on life and technology

[ chris stevenson ]


[ joe walnes ]

joe's new jelly

[ rob baillie ]


Powered by Blosxom

Registered plugins: SmartyPants, antispam, bloglinks (v0.2), calendar (v0+6i), pluginfo (v1.0), and userstatus (v0.1)
This work is licensed under a Creative Commons License.