I am trying to explain to my team why this is bad practice, and I am looking for a link to the anti-pattern to help in my explanations. This is a very large enterprise application, so here is a simple example to illustrate what was implemented:
public void ControlStuff() { var listOfThings = LoadThings(); var listOfThingsThatSupportX = new string[] {"ThingA","ThingB", "ThingC"}; foreach (var thing in listOfThings) { if(listOfThingsThatSupportX.Contains(thing.Name)) { DoSomething(); } } }
I suggest adding a property to the Things base class to let us know if it supports X, since the Thing subclass will have to implement this functionality. Something like that:
public void ControlStuff() { var listOfThings = LoadThings(); foreach (var thing in listOfThings) { if (thing.SupportsX) { DoSomething(); } } } class ThingBase { public virtual bool SupportsX { get { return false; } } } class ThingA : ThingBase { public override bool SupportsX { get { return true; } } } class ThingB : ThingBase { }
So, it’s pretty obvious why the first approach is bad practice, but what is it called? Also, is there a sample more suitable for this problem than the one I offer?
c # anti-patterns
John Cornell Oct. 06 '11 at 7:01 2011-10-06 07:01
source share