How to solve the "Growing If Statement" problem?

Posted by Achilles on Stack Overflow See other posts from Stack Overflow or by Achilles
Published on 2010-06-08T13:43:34Z Indexed on 2010/06/08 13:52 UTC
Read the original article Hit count: 209

I've been doing some reading about design patterns and wanted some perspective. Consider the following:

Dim objGruntWorker as IGruntWorker

if SomeCriteria then
   objGruntWorker = new GoFor()
else if SomeOtherCriteria then
   objGruntWorker = new Newb()
else if SomeCriteriaAndTheKitchenSink then
   objGruntWorker = new CubeRat()
end if

objGruntWorker.GetBreakfast()
system.threading.thread.sleep(GetMilliSecondsFromHours(4))
objGruntWorker.GetLunch()

The above code grows each time a new Criteria arises. I've seen code like this all over the place and in ignorance wrote some of it myself. How should this be solved? Does this kind of anti-pattern have a more "formal" name? Thanks for your help!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about best-practices