Skip to main content
2 of 5
added 729 characters in body
Ewan
  • 84.5k
  • 5
  • 91
  • 189

Put your conditions in objects and put those objects in a list

foreach(var condition in Conditions.OrderBy(i=>i.OrderToRunIn)) { if(condition.EvaluatesToTrue()) { addAlert(condition.Alert); break; } } 

Edit --

if multiple actions are required on condition you can do some crazy recursion

void RunConditionalAction(ConditionalActionSet conditions) { foreach(var condition in conditions.OrderBy(i=>i.OrderToRunIn)) { if(condition.EvaluatesToTrue()) { RunConditionalAction(condition); break; } } } 

Obviously yes. This only works if you have a pattern to your logic. If you try to make a super generic recursive conditional action then the setup for the object will be as complicated as the original if statement. You will be inventing your own new language/framework.

But your example does have a pattern

Ewan
  • 84.5k
  • 5
  • 91
  • 189