Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help refactoring this C# function

Tags:

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! =)

like image 795
nedrossUK Avatar asked Aug 14 '10 12:08

nedrossUK


People also ask

What is refactoring in C?

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.

What is refactoring for?

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.

How do you refactor code in VS code?

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


1 Answers

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

On (not) thinking negatively

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

Additional tips

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.

See also

  • C# ‘or’ operator?
  • What is the diffference between the | and || or operators?
  • What is the best practice concerning C# short-circuit evaluation?
  • Short circuiting statement evaluation — is this guaranteed? [C#]
  • When to use ^ operator
like image 110
polygenelubricants Avatar answered Nov 09 '22 02:11

polygenelubricants