Refactoring long if - else if - else with common objects
c#, design-patterns
Solution
You could break the large scope blocks into separate functions.
if(IsHardWrappedCandy(thing))
ValidateHardWrappedCandy(thing);
else if (IsChocolateCandy(thing))
ValidateChocolateCandy(thing);
There is also interitance, where you would create candy classes and encapsulate behavior:
public abstract class CandyBase
{
public abstract void Validate();
}
public class HardWrappedCandy : CandyBase
{
public override void Validate()
{
// TODO: Validation logic
}
}
Then your code would be:
foreach(var candy in candies)
candy.Validate();
Of course you will need to standardize parameters and such, but you get the idea.
Read the book Clean Code, it has a lot of great ideas on how to refactor. http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
Problem
I'm looking for a way to refactor a log if/else if/else statement that also has a bit of nesting. The blocks also use quite a few common objects. My aim is to break the code apart to manageable units extracted to different classes and make it pluggable in case I need to cover a new condition. Here's some dummy code to illustrate: ``` List<ValidationResult> validationResults = new ...; Inspector inspector = commonInspector; bool additionalOp = commonFlag; HashSet<Thing> thingsToCheck = theThings; foreach (var thing in thingsToCheck) { if (IsCandy(thing) && thing.IsWrapped && thing.IsHard) { var inspected = inspector.Inspect(thing.Value); if (inspected != thing.Value) { validationResults.Add(new ...); if (additionalOp) { thing.Taste(); } } } else if (IsChocolate(thing)) { var sweet = (Sweet)thing; List<BadCalories> badCalories; while (var calorie in sweet.Calories) { if (calorie.IsGood) continue; badCalories.Add(calorie); } foreach (var badCal in badCalories) { var inspected = inspector.Inspect(badCal.Value); if (inspected != badCal.Value) { validationResults.Add(new ...); if (additionalOp) { badCal.Taste(); } } } } else { if(thing ...) else if (thing ...) } ``` I read a bunch of articles/SO posts of various patterns/practices that may apply, but the code's dependencies complicate it a bit for me to apply the concepts. It doesn't help that I've been looking at the code somewhat too closely for a while now so it's hard to break out from micro-managing to a birds eye view.