Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose i have nested if condition like arrow
C#
if()
{
   if()
      if()
        .....
        .....


}
else
{
}

How can I replace above problem with design pattern or some elegant approach
i have heard something about rule engine but don't know how to implement
Posted
Updated 23-Oct-14 10:20am
v2
Comments
Sergey Alexandrovich Kryukov 23-Oct-14 15:36pm    
The well-known concept of "design pattern" is about much less fine-grain, much wider scale features.
What's wrong with just reading the language manual and reference?
—SA

Please see my comment to the question. Such techniques are not related to "design patterns".

This is way too simple thing. Suppose you have
C#
if (conditionOne)
   if (conditionTwo)
      if (conditionTree)
          //...

It is strictly equivalent to
C#
if (conditionOne && conditionTwo && conditionTree) // in exactly this order
   //...

Note the use of '&&', which is not the same as '&'. '&&' optimizes out redundant condition check, exactly as with nested "if". In contrast to that, '&' will perform all the checks. Not only it would be worse for performance, if makes some difference in calculation if those conditions are properties with side effects (or they could functions, if I wrote conditionOne() instead of using the property syntax). That's why it is not recommended to create properties with side effect on getters; and even less recommended to use properties of functions with side effects in conditional operators. (I hope you understand what is "side effect": something except just returning the value).

Please see:
http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^],
http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Naser Hassan 23-Oct-14 16:10pm    
i was looking some design pattern or technique by which i can make this code more readable and usable .lets say i want to use this conditions as rule what if my rule changed ,so wherever i have applied this rule needs modification
CHill60 23-Oct-14 16:22pm    
Perhaps if you described your "rule engine" in a little more detail? As you have phrased your question, Sergey has already shown you how to make it more readable/elegant/accurate. Use the Improve Question link to give more detail perhaps?
PIEBALDconsult 23-Oct-14 16:23pm    
Design Patterns won't help with that. If you want a better answer, ask a better question.
Sergey Alexandrovich Kryukov 23-Oct-14 16:24pm    
I answered your question in full, didn't I?

If your rule need modification, the question is: during development of compile time? If development, just change the rules. Yes, make your code more readable. Just look at two equivalent form and decide by yourself what is readable and what not. For readability, consider breaking overly complex Boolean expressions into sub-expressions assigned to some local Boolean variables. I hope it's clear.

If runtime, use some Boolean properties or functions in the condition; they will return different Boolean values, depending on required factors. This are all your "patterns".

Will you accept it formally (green "Accept" button)? In all cases, your follow-up questions will be welcome.

—SA
Naser Hassan 23-Oct-14 16:34pm    
i agree with your solution what you gave but i have many If's in my code one of which i asked ..sorry i am very new in software development so i don't know much
Even the question is answered and an answer is accepted...
If you are really interested to learn these "boolean" stuff I suggest to dive into it's Background.

E.g. you can start here: http://en.wikipedia.org/wiki/Boolean_algebra[^]

To know the Background also avoids to write silly staemanets like one can see often:

C#
bool theDecision= a && b;
if (theDecision == true) // silly to compare a boolean like this
{
    // this and that
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900