Is there a way to quickly check the following logic? I'm using C#.
if(a)
{
}
if(b)
{
}
if(c)
{
}
else none of the above //...? execute if all above conditions are false
{
}
This differs from using if-else in that a, b, and c can all be true at once. So I can't stack them that way. I want to check else for a, b, and c are all false without writingif(!a && !b && !c)
. This is because the code can get quite messy when the if conditions become more complex. It requires rewriting a lot of code. Is this possible?
Rather than encapsulate some complex condition in a method that you will only ever call once or twice, I would just keep in a variable. This is also more readable than using some marker boolean as other answers suggest.
A contrived example,
Now we have abstracted what might otherwise be complex conditions into readable English!
Well it's not pretty "clean", but I'd do
Also, if your conditions are really pretty big, I recommend the rule G28 (Encapsulate Conditionals) from the book Clean Code from Robert C. Martin
It is pretty verbose, but can be easier to read in some instances :
How about combine the concepts of Strategies and Specifications