Non-functional Code Determines the quality of the Code.
Use the De Morgan rule to understand the code Category: C ++ 53 reading Comment (0) Favorites Report
Today, I am reading code reading methods and practices. I found it interesting to use Morgan's law to analyze the code. I searched it online.
Bytes -----------------------------------------------------------------------------------------------------------------------
De Morgan rule:
- ! (X & Y) →! X |! Y
- ! (X | Y) →! X &&! Y
How can this be used?
For example, let's look at the code for finding the minimum value of a three-digit number:
-
- If (x <Y & x <z)
-
- Min = X;
- Else if (Y <z)
-
- Min = y;
-
- Else
-
- Min = z;
Many beginners (including me in the past) feel difficult to understand the above logic and always feel like nothing.
Let's take a look at the De Morgan rule,
The first if clause is used to judge x <Y & x <Z. If it is true, the minimum value is X. This is easy to understand. The key is to execute the following else if (Y <z) what are the conditions? Naturally
- ! (X <Y & x <z) → x> = Y | x> = z
So if you run this step, it means that X must be greater than Y and Z, so it must not be the minimum value, next, you just need to judge the size of Y and Z.