Thursday, 5 November 2009

Killing the Config

Most applications require config of some description and it is considered good practice to wrap any calls which retrieve configured values in a config class and pass that around rather than splattering the codebase with direct calls to the frameworks inbuilt static methods.

With all the applications configs neatly wrapped in one place the code starts to become more maintainable. Yet are we just replacing one evil with another, if somewhat lesser, one? The problem I often see is this Config class finds itself passed from class to class flouting it's promiscuity and leading writers of articles on config objects down paths dangerous and full of inappropriate metaphors more commonly favoured by the ruby crowd.

There is no doubt that it is useful to collate all of the configurable elements together in one (or more) common classes. What should be avoided is leaking this concept through the whole domain and kicking it from object to object like a dirty old data bag which everyone rumages through, unchecked, pulling out stuff on their own whim. Config is an implementation detail which the majority of the application should have no concept of and (like any other such implementation details) it needs to be locked firmly away like a lump of kryptonite in a lead cased layer so it can no longer leak and permeate and weaken your SuperApp.

Let's think about this in a BDD fashion. When your class want's a ghostbusting url who's it gonna call (they have a webpage now dinyuknow)? string Config.GhostBusterUrl? Or perhaps the GhostBusterUrl class? If your class needs a typed collaborator with certain logic and behaviour (even if it is just supplying a valid url) then that's what your class should get. Not some smelly generic config object that dishes out dirty unparsed strings. The tests should be enough to tell you I'm telling you the right thing: several lines of priming the mockConfig are replaced with the simple supply of a GhostbusterUrl.

So how do we get the GhostBusterUrl from a line in config to those poor desperate classes who are being terrorized by spooks? With a little Tell don't ask and a container. Turn the config object inside out, make its role to inject into the application the dependencies it wants to supply. If your config believes it has a GhostBusterUrl then get it to construct one and register it (along with anything else it thinks it might have of use) into the container when the application tells it to. Not only does the config object gain some of its dignity back (some other classes were calling it nasty names, I know, it's the new millennium, but some people still hold the old fashioned ideas) and is safely kept right to the very edges of your application where no one can get at it.

Another positive side effect is that now your config object is giving birth to loads of lovely typed objects that actually do real stuff rather than having wandering hands pluck at its primitives (I know but I did warn it was a dangerous path laden with tasteless innuendos, I was bound to trip). Though of course, be careful not to pollute your codebase with a million classes that simply represent strings and do nothing else (it's a fine line and one you'll need to argue between me, myself and I). It's also one of the easiest refactorings you can do to your code. Give it a go (and if you object on the grounds of their being loads of small types then please find the closet crumbling procedural bridge and throw yourself into the polluted sequential river below).

7 comments:

Anonymous said...

[url=http://www.kamagrashop.pl]viagra[/url], foil my site. If you are interested in beau, perceive [url=http://www.4uescort.de]huren[/url] or [url=http://www.escort4u.pl]ogloszenia towarzyskie[/url]

Rakesh said...

Apart from being the pride of Malaysia, Kuala Lumpur is considered to be the "Gateway to South East Asia". The city simply oozes with sky kissing edifices, stylish shopping malls and rejuvenating spas. What adds further to the popularity of the metropolis thereby inducing millions to book Flights to Kuala Lumpur , Cheap Flights to Kuala Lumpur and Kuala Lumpur Flights is the electrifying nightlife of the conurbation.
Cheapest Air Tickets

Anonymous said...

Post18, http://www.arlo.net/massacree/ online viagra, fzqc9, http://www.arlo.net/fccgb/ viagra no prescriptions, dcdw1, http://www.arlo.net/fccgb/notes/ buy generic viagra, myys5, http://www.arlo.net/bytes/ cheap viagra, wlcn2, http://www.arlo.net/live/ viagra for sale

Anonymous said...

A 2006 study held in Korea showed that there was a significant decrease in the waist circumference and body weight of individuals given phentermine as compared to those given placebo Psychologists from Edinburgh Napier University surveyed 200 students on their use of Facebook[url=http://www.victorcruzjerseys.net]Victor Cruz Authentic Jersey[/url]
and found that a for a significant number of users the negative effect of the social network outweighed the benefits of staying in touch with friends and family Feel your power authoritiesOakImage SourceThe oak is a common symbol of strength and endurance The Horned Frogs were led by senior QB Andy Dalton who had a great game[url=http://www.officialredskinsnflstore.com]Robert Griffin III Jersey[/url]
throwing for 217 yards and accounting for two of their three touchdowns
Since the human mind is wired to ignore the usual[url=http://www.AlfredMorrisJersey.net]Alfred Morris Womens Jersey[/url]
people categorize the same looking slides as The difference between one’s probability and a sport book’s cost probability has to be positiveSince World population reached billion in [url=http://www.officialredskinsnflstore.com]www.officialredskinsnflstore.com[/url]
average million people increase annually;and the global population will reach billion to the end of In addition to population growth[url=http://www.AlfredMorrisJersey.net]Alfred Morris Authentic Jersey[/url]
economic growth in developing countries boost the demand for food What each employee looks at can also be trackedmetadata display: block;width: 100%;clear: both;margin-top: 8px;padding-top: 12px;height: 65px; p#17) @DaveRamsey (Up from #23) - Dave Ramsey - Official Twitter for Dave Ramsey[url=http://www.AndrewLuckJerseynike.com]www.AndrewLuckJerseynike.com[/url]
New York Times best-selling author and host of The Dave Ramsey Show Taking three underdogs in week six!Tennessee +3 @ Tampa BayWho’s real? Who’s fake? Two of the league’s top defenses thus far go head-to-head in Tampa Bay

Anonymous said...

[url=http://www.49ersonlineofficialstore.com/frank_gore_jersey_super_bowl]Frank Gore Jersey[/url] emurbursofs
[url=http://www.patriotsfootballprostore.com/brandon_spikes_jersey_super_bowl]Brandon Spikes Jersey[/url] TynckeyncBype
[url=http://www.footballfalconsproshop.com/roddy_white_jersey_super_bowl]Roddy White Jersey[/url] Proonseorek

Anonymous said...

top [url=http://www.c-online-casino.co.uk/]www.c-online-casino.co.uk[/url] check the latest [url=http://www.realcazinoz.com/]realcazinoz[/url] autonomous no store perk at the foremost [url=http://www.baywatchcasino.com/]casino online
[/url].

Anonymous said...

* Very, Very Slightly IncluFeF category (VVS) FiamonFs have minute inclusions that are FiFFicult For a skilleF graFer [url=http://www.germanylovelv.com/]louis vuitton knolckoffs[/url]
see Fewer than 10 x magniFication[url=http://www.germanylovelv.com/]louis vuitton knolckoffs[/url]
The VVS category is FiviFeF in[url=http://www.germanylovelv.com/]louis vuitton knolckoffs[/url]
two graFes; VVS1 Fenotes a higher clarity graFe than VVS2. Pinpoints anF neeFles set the graFe at VVS.Most oF the people throng [url=http://www.germanylovelv.com/]Louis Vuitton Outlet[/url]
online browsing outlets in InFia as they recuperate Fiscounts than what the marketplace oFFer[url=http://www.germanylovelv.com/]Louis Vuitton Outlet[/url]
You get optimum selection oF proFucts anF solutions unFerneath FiFFerent branFs that you're spoilt For preFerence. The comparison among excellent anF prices gets hassle-Free anF purely natural consequently you coulF avail iFeal oFFers at most inexpensive pricehttp://www.germanylovelv.com/

|
|

About Me

My photo
West Malling, Kent, United Kingdom
I am a ThoughtWorker and general Memeologist living in the UK. I have worked in IT since 2000 on many projects from public facing websites in media and e-commerce to rich-client banking applications and corporate intranets. I am passionate and committed to making IT a better world.