Dependency Injection, Patterns and Fashion in Software Engineering

Rusty Elliote Harold has an amusing comment on Guice and how dependency injection contributes to code obfuscation. I agree with him, though the IDE can make the navigation of the where beans are coming in easier, for instance IntelliJ has it all over eclipse in this area, but, for spring anyway, it usually means going back and forth between xml and java code and then finding that it craps out at runtime rather than compile time which removes one of the benefits of a strongly typed compiled language. Harold writes:

I've been using Guice at my day job for the last year, and not by choice. I find it makes our code base far harder to comprehend and work with. ... As far as I've been able to see, all dependency injection does for us is make unit testing edge cases a little easier ...

We haven't been using dependency injection for unit testing, so I don't have any experience there. One of the pains of complex software systems is configuration. It is a stumbly mess where you to get things running perfectly you have to modify this, test, modify that, test, etc etc. We mainly use dependency injection in a configuration sense. Our software engineers are more comfortable working in code itself, so it is often easier just to explicitly put a reference in a class anyway as it is directly in their competency domain.

A good example of that is how developers when doing dhtml often stack their javascript with all sorts of CSS information. I have seen that in the past, and it is simply because they are more comfortable manipulating CSS from within the language of javascript than CSS itself. That is bad design, javascript shouldn't do anything more than display:none and display:"" along with changing the class attribute. However it is a good example of how software developers are more comfortable in the code itself, than in configuration. Harold continues:

One should not complexify a class's API purely to make testing easier. Testing exists to serve the model code, not the other way around. To cite the classic dependency injection example, if the only reason you pass a Clock object to a constructor instead of creating one privately inside the class is to support unit testing, then don't do it. Unit testing is no excuse for heavily coupled classes exposing their private parts and violating encapsulation. When forced to choose between good object oriented design and testability, choose good object oriented design.

We are hitting similar issues in our project. We are having to make design compromises in encapsulation simply to expose variables to automated unit testing.

I often think that many of these conventions are applied too widely, software being software and largely a young person's game, it is hopelessly exposed to software hipsterish tech fashion. DI is cool, lets make everything DI! Patterns are cool, lets never speak in plain English again, and lets make all our objects and structures adhere to the patterns in the GoF book!

I hate it when you are talking about structures and the pattern language gets in the way. For instance I was talking with a co-worker about undo which is usually expressed in the Command Pattern (and which we implemented in another system that way). However in PureMVC the command patterns are used as Controllers. And because of that we had a disconnect about what were commands and what were controllers.

I also dislike when someone is talking about software and discusses a pattern they are intimately familiar with, and I am not (the GoF book has 22 patterns in it), and I think, "Oh man, don't make me go and look up a book to work out what we are talking about." I keep the GoF book on top of my powermac, I am the only one in the office that does that, and I use it as a reference when other people throw patterns at me that I am not familiar with.

I am sure that half of all pattern discussion is done from ignorance, or with the one talking about the pattern not being absolutely certain what it actually is; "We should put an Observer in here, or a Visitor, maybe a Cromulator and Confabulator would be better!"

There is a reason that every software interview asks about singletons. It is the only one simple enough to remember off the top of your head. Also, because of that, I think singleton is hopelessly over-used in software simply because it is the simplest pattern structure, just one object, that a developer can use. As a result good Object Oriented structures are often littered with getInstance() or even static create() methods.

I don't know. There is a time and place, and all those folkisms, is probably a good summary for what I have written above. Use in moderation and without an eye to fashion or doctrine. Disclaimer, disclaimer ...
Permalink, Dependency Injection, Patterns and Fashion in Software Engineering, May 2009, cam

More Reading on Software engineering

Most Popular on South Sea Republic

The articles that have been viewed the most:

Most Popular Restaurants in Phoenix

Phoenix Eats Out is the restaurant review site for Phoenix, Scottsdale and Old Town Scottsdale which lists the modernist and contemporary restaurants, taverns and bars in the greater Phoenix area. This is the list of the most popular restaurants pages from phoenixeatsout.com that have been viewed the most; My personal favourite restaurants in Phoenix are AZ88, Postinos, Bomberos with Grazie, Humble Pie, Orange Table, The Vig, Fez and others coming close behind. View the complete list with the photo-journalistic style images on phoenixeatsout.com

Most Popular Hikes in Arizona

Arizona is an outdoor state and has lots of hiking in the city and around the state. Phoenix is unusual for most cities in having several large mountains in the center of the city with great hiking. Anyone who comes to Phoenix has to do the Echo Canyon trail on Camelback and the Summit Hike on Squaw Peak or Piesta Peak. The views of the city, suburbs and surrounding mountains are wonderful from Camelback and Piesta Peak. For more experienced hikers there is the McDowell Mountains in North Scottsdale that has several difficult and strenuous hikes in Tom's Thumb and Bell Pass. Alternatively, you can hike the highest mountain in Arizona. At 12,600 feet Humphrey's Peak is a long and difficult hike.

Alternate Australian Constitutions

Between 2004 and 2009 this site, southsearepublic.org, was a constitutional blog based on scoop which focused on Australian and global constitutional issues. One of the strongest aspects of it was the development of constitutions by those involved in the blog. These constitutions are the outcome: The constitutions were built using principles from Montesquieu's separation of powers, the enlightnment's universal political rights and the ancient Athenian technology of sortition and choice by lot.

Archives For South Sea Republic

South Sea Republic started in 2004 as an Australian constitutional blog in 2004 based on scoop software. It was an immigrative outgrowth of Kuro5hin. The archives for each year since then; The articles are ordered by views.

Who Is Cam Riley

Cam Riley I am an Australian living in the United States as a permanent resident. I am a software developer by trade and mostly work in Java and jump between middleware and front end. I originally worked in the New York area of the United States in telecommunications before moving to Washington DC and working in a mix of telecommunications, energy and ITS. I started my own software company before heading out to Arizona and working with Shutterfly. Since then I have joined a startup in the Phoenix area and am thoroughly enjoying myself.

I do a lot of photography which I post on this website, but also on flickr. I have a photo-journalistic website which lists the modernist and contemporary restaurants in phoenix. I have a site on the Australian Flying Corps [AFC] which has been around since the 1990s and which I unfortunately lost the .org URL to during a life event; however, it is under the www.australianflyingcorps.com URL now. The AFC website has gone through several iterations since the 90s and the two most recent are Australian Flying Corps Archives(2004-2002) and Australian Flying Corps Archives(2002-1999) which are good places to start.

Websites Worth Reading

Websites of friends, colleagues and of interest;