Skip to main content
EDIT: monikers are not required. Every post on Stack Exchange has a detailed edit history that anyone can view.
Source Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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 --

ifIf 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

Edit --

A common use case for this pattern would be validation. Instead of :

bool IsValid() { if(condition1 == false) { throw new ValidationException("condition1 is wrong!"); } elseif(condition2 == false) { .... } 

Becomes

[MustHaveCondition1] [MustHaveCondition2] public myObject() { [MustMatchRegExCondition("xyz")] public string myProperty {get;set;} public bool IsValid() { conditions = getConditionsFromReflection() //loop through conditions } } 

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

Edit --

A common use case for this pattern would be validation. Instead of :

bool IsValid() { if(condition1 == false) { throw new ValidationException("condition1 is wrong!"); } elseif(condition2 == false) { .... } 

Becomes

[MustHaveCondition1] [MustHaveCondition2] public myObject() { [MustMatchRegExCondition("xyz")] public string myProperty {get;set;} public bool IsValid() { conditions = getConditionsFromReflection() //loop through conditions } } 

It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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; } } 

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

A common use case for this pattern would be validation. Instead of :

bool IsValid() { if(condition1 == false) { throw new ValidationException("condition1 is wrong!"); } elseif(condition2 == false) { .... } 

Becomes

[MustHaveCondition1] [MustHaveCondition2] public myObject() { [MustMatchRegExCondition("xyz")] public string myProperty {get;set;} public bool IsValid() { conditions = getConditionsFromReflection() //loop through conditions } } 
added 176 characters in body
Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

Edit --

A common use case for this pattern would be validation. Instead of :

bool IsValid() { if(condition1 == false) { throw new ValidationException("condition1 is wrong!"); } elseif(condition2 == false) { .... } 

Becomes

[MustHaveCondition1] [MustHaveCondition2] public myObject() { [MustMatchRegExCondition("xyz")] public string myProperty {get;set;} public bool IsValid() { conditions = getConditionsFromReflection() //loop through conditions } } 

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

Edit --

A common use case for this pattern would be validation. Instead of :

bool IsValid() { if(condition1 == false) { throw new ValidationException("condition1 is wrong!"); } elseif(condition2 == false) { .... } 

Becomes

[MustHaveCondition1] [MustHaveCondition2] public myObject() { [MustMatchRegExCondition("xyz")] public string myProperty {get;set;} public bool IsValid() { conditions = getConditionsFromReflection() //loop through conditions } } 
added 176 characters in body
Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

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

Edit : It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it.

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

added 729 characters in body
Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189
Loading
Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189
Loading