Skip to content

De Morgan's Laws

Sourcery refactoring id: de-morgan

Description:

Simplifies conditional logic using De Morgan's laws

Before:

if not not p:
    a = 1
if not a is b:
    a = 1
if not (p == 1 and q == 2):
    a = 1

After:

if p:
    a = 1
if a is not b:
    a = 1
if p != 1 or q != 2:
    a = 1

Explanation:

When reading logical statements it is important for them to be as simple as possible. This proposal applies De Morgan's laws to simplify code by removing double negatives or pushing negations deeper into statements.