I have written functions that look like this:
bool IsDry(bool isRaining, bool isWithUmbrella) { if (isRaining) { if (isWithUmbrella) return true; else return false; } else return true; }
I need to check, if it's raining, then the person needs to carry an umbrella in order to keep dry (don't laugh, this is just an example, our actual business rules are more serious than this).
How can I refactor this, because right now it looks clumsy.
Thanks for the help, guys! =)
Refactoring or Code Refactoring is defined as systematic process of improving existing computer code, without adding new functionality or changing external behaviour of the code. It is intended to change the implementation, definition, structure of code without changing functionality of software.
Refactoring enables continuous improvement of business application logic, making the code easier to work with and to modify. This enables code to evolve over time and for organizations to incrementally implement features or technology that improves the code performance or maintainability.
Clicking on the Code Action lightbulb or using the Quick Fix command Ctrl+. will display Quick Fixes and refactorings. If you'd just like to see refactorings without Quick Fixes, you can use the Refactor command (Ctrl+Shift+R).
It looks like the business rule you're trying to enforce is:
P IMPLIES Q
This is logically equivalent to:
(NOT P) OR Q
Thus, you can simply write:
bool IsDry(bool isRaining, bool isWithUmbrella) { return !isRaining || isWithUmbrella; }
Depending on the predicate, it may also be simpler to first think in terms of its negation.
NOT (P IMPLIES Q)
We now substitute the identity above:
NOT ((NOT P) OR Q)
Now we can apply DeMorgan's Law:
P AND (NOT Q)
Since this is the negation, we must negate this to get back to the positive. The double negation may seem confusing at first, but going back to the example, we have:
bool IsDry(bool isRaining, bool isWithUmbrella) { bool isWet = (isRaining && !isWithUmbrella); return !isWet; }
Here are some examples of common boolean
expression rewriting:
BEFORE | AFTER ________________________________________|________________________________________ | if (condition == true) ... | if (condition) ... ________________________________________|________________________________________ | if (condition == false) ... | if (!condition) ... ________________________________________|________________________________________ | if (condition) { | return condition; return true; | } else { | return false; | } | ________________________________________|________________________________________ | if (condition1) { | return (condition1 && condition2 if (condition2) { | && condition3); if (condition3) { | return true; | } else { | return false; | } | } else { | return false; | } | } else { | return false; | } | ________________________________________|________________________________________ | return (condition1 && !condition2) || | return condition1 != condition2; (condition2 && !condition1); | // or condition1 ^ condition2;
Note that the predefined ^
in C# is the exclusive-or operator, even for integral types (i.e. it's not an exponentiation operator). The predefined &&
and ||
are conditional logical operators that performs "short-circuit" evaluation.
^
operatorIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With