DEV Community

Cover image for Write Simpler Logical Expressions In Your Code with These 2 Rules!
Oluwawunmi Adesewa
Oluwawunmi Adesewa

Posted on

4 1 2 2 1

Write Simpler Logical Expressions In Your Code with These 2 Rules!

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)
Enter fullscreen mode Exit fullscreen mode

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):
Enter fullscreen mode Exit fullscreen mode

Using De Morgan’s Law, you can rewrite it as:

if not x > y or not y < z:
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode
  • Writing Clearer Logical Expressions.
#Before
if (!(a && b)) { console.log("True"); }
#After
if (!a || !b) { console.log("True"); }
Enter fullscreen mode Exit fullscreen mode

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay