When writing code, it's easy to get stuck in complex if
statements and nested conditions, where a single misplaced not
or and
can result in bugs and errors.
But what if you could simplify logical statements and make your code more maintainable? This article shares two key rules in mathematical logic that simplify the process of writing clearer and simpler logical statements in programming.
Introduction
Consider a scenario where you need to negate a logical statement with multiple AND
or OR
operators.
(A and B) or (C and D)
Manually negating this expression can be tricky because logical operators have precedence rules, and distributing them incorrectly can cause a bug in your code.
De Morgan's law simplifies this process by transforming expressions like these into more manageable forms, making it easier to write and understand complex logical statements
What Is De Morgan’s Law?
De Morgan’s Laws are two transformational rules in mathematical logic that allow you to switch between AND
and OR
when negating an expression.
Here's how it works
- NOT (A AND B) = (NOT A) OR (NOT B)
- NOT (A OR B) = (NOT A) AND (NOT B)
Note: The notation for these two rules may vary slightly depending on the programming language or context, but the principle remains the same.
These laws can be applied in programming to write clearer conditional statements. For example,
Consider this statement:
if not (x > y and y < z):
Using De Morgan’s Law, you can rewrite it as:
if not x > y or not y < z:
This implies that conditional statements can be written in different ways to the same effect. As seen in the above example, when negating a statement with and
change it to or
and negate each condition. For or
, change it to and
and negate each condition.
These transformations, while seemingly minor, make complex logical expressions clearer and easier to maintain in code.
Take a look at this loop condition:
while (selection != 'Q' || selection != 'q')
This loop condition appears logical but it has a flaw because the OR operator returns true if at least one of the conditions is true. This creates an infinite loop.
Using De Morgan’s second law:
while (selection != 'Q' && selection != 'q')
Now, the loop correctly exits when selection is either Q or q.
Apply De Morgan’s Law When:
- Simplifying Nested Negations.
#Before
if not (a and b and not c):
print("Condition met")
#After
if not a or not b or c:
print("Condition met")
- Writing Clearer Logical Expressions.
#Before
if (!(a && b)) { console.log("True"); }
#After
if (!a || !b) { console.log("True"); }
Both conditions evaluate the same way, but the second version is easier to read.
Conclusion
While you still need to understand the rules and precedence of logical operators, De Morgan's law provides clear methods to break down and improve logical expressions into more manageable forms that require less mental effort to understand.
So, the next time you find yourself dealing with nested conditions and negations, apply these two simple rules to clean up your logic and avoid unnecessary bugs.
For a more comprehensive reading on De Morgan's law and it's usage in logical expressions, check here.
Top comments (0)