Course contentsShow
← Software Engineering
Lesson 387 of 1,93311. Code Quality and ReadabilityFree lesson

Simplifying Boolean Logic

Techniques for making complex boolean expressions clearer through extraction, De Morgan's laws, and truth tables.

Simplifying Boolean Logic

You'll learn how to transform tangled boolean expressions into clear, readable logic that reveals its purpose at a glance.

Complex boolean expressions are like trying to navigate with directions full of double negatives: "Don't not turn left unless it's not Tuesday." Your brain has to work overtime to parse what's actually being asked.

The Core Challenge

When conditions pile up with &&, ||, and ! operators, the logic becomes opaque:

if (!isWeekend && !(hour < 9 || hour > 17) && !isHoliday) {
  // open for business
}

What does this actually check? It takes mental effort to decode.

Technique 1: Extract to Named Variables

Give meaningful names to sub-expressions:

let isWeekday = !isWeekend;
let isDuringBusinessHours = !(hour < 9 || hour > 17);
let isRegularDay = !isHoliday;

if (isWeekday && isDuringBusinessHours && isRegularDay) {
  // open for business
}

Technique 2: Apply De Morgan's Laws

These mathematical rules transform negated conditions:

  • !(A && B) becomes !A || !B
  • !(A || B) becomes !A && !B

Our earlier example simplifies:

let isDuringBusinessHours = hour >= 9 && hour <= 17;

Much clearer than !(hour < 9 || hour > 17)!

Technique 3: Extract to Functions

For repeated or complex logic:

function isOpenForBusiness(date, hour) {
  return !isWeekend(date) 
    && hour >= 9 && hour <= 17 
    && !isHoliday(date);
}

When to Use Truth Tables

For truly complex boolean logic with multiple inputs, sketch a truth table on paper. It reveals patterns and helps you find the simplest equivalent expression.

Key Takeaway: Simplify boolean logic by extracting meaningful names, applying De Morgan's laws to remove unnecessary negations, and encapsulating complex conditions in well-named functions—your future self will thank you.