I agree with svick's comment completely. In some cases, the following approach can be good (but not to reduce cyclomatic complexity, generally to create pluggable decision-makers):
public class SwitchAction{
public Func<bool> Predicate { get; set; }
public Action TheAction { get; set; }
}
public List<SwitchAction> SwitchableActions = new List<SwitchAction>();
public void InitialiseSwitchableActions()
{
SwitchableActions.AddRange(new[] {
new SwitchAction() { Predicate = () => Name.Text == string.Empty,
TheAction = () => Name.Background = Brushes.LightSteelBlue },
new SwitchAction() { Predicate = () => Age.Text == string.Empty,
TheAction = () => Age.Background = Brushes.LightSteelBlue },
});
}
public void RunSwitchables()
{
var switched = SwitchableActions.FirstOrDefault(s => Predicate());
if(switched != null)
switched.TheAction();
else
//TODO: something else.
}
Of course - if actually these actions are not mutually exclusive you have to change that last method a little bit:
public void RunSwitchables()
{
bool runCatchAll = true;
foreach(var switched in SwitchableActions.Where(a => a.Predicate())
{
switched.TheAction();
runCatchAll = false;
}
if(runCatchAll)
//TODO: Something else.
}
Are either of these more readable though? Hmm... probably not.