Supercharge your logic design using Boolean Algebra

Recently, while working on a project, there was a module where I had to design a to-do list. It was just like any other To-Do list, where you have list of tasks, initially all pending, and as you finish them, you mark them complete.

Just another To-Do list

However, there was a catch. The client wanted two toggle buttons to filter pending and complete tasks. As baffling as it might seem from the UX perspective, since a radio button would have done just fine, it was a requirement of the project.

The logic to implement this might seem pretty simple and straightforward to you, but if you dig deeper, you see a bigger problem. Let’s start with our list of tasks.

Array of task objects

To filter this array, we have only three criteria, the search string, and the pending and complete buttons. Cool, let’s write a simple if-else ladder and get going.

Basic Filter for To-Do
To-Do App with basic filter

Wait… it isn’t working. Why? You can tinker around with the pen if you wish to figure out the answer yourself, or you can skip below.

As mentioned before, there are three criteria for our filter. To understand where we went wrong, let’s think of our element as a car, and the filters as toll stops. Now, to get correct result, our car must clear all three toll stops. However, our current implementation allows the car if it clears any one of the toll stops. You can verify this by disabling pending and complete check-boxes and then typing in the search bar. Since we closed the remaining two toll stops, all traffic is rerouted to the remaining toll stop. So, any car which cannot pass it is eliminated.

Our car can pass successfully if it clears at least one toll stop

Alright then, lets change our code to make the car pass all three stops to reach its destination.

Modified To-Do filter (serial)
To-Do App with Serial Filter

Hey… its still not working. The above scenario, although simplified, is a valid example of how writing complex logic can get really tricky at times. You can brainstorm for hours to derive a logic, but you still cannot be sure it will work for all scenarios. Here comes, Boolean algebra to the rescue. To solve this problem, let’s draw a truth table first.

From the truth table we can clearly see that our output (F) is true only in 4 cases. From this table, we can derive our filter criteria as follows (assuming pending is 0 and complete is 1 in column B):

  1. !B.C.!D.E = F
  2. !B.C.D.E = F
  3. B.!C.D.E = F
  4. B.C.D.E = F

Which will generate a circuit like this (the switches on the left end are B, C, D and E while the ones next to them are !B, !C, !D and !E)

Circuit representation of logic

Here you can see the first condition (!B.C.!D.E = F) getting demonstrated using AND and OR gates. If we take the same logic and use it in our code, it will work just fine…

Logic derived to filter list using Boolean algebra

… or we can simplify it. Let’s recall the laws of Boolean algebra we studied in college. You can check them online at

Applying the laws on our equation,

!B.C.!D.E + !B.C.D.E + B.!C.D.E + B.C.D.E = F

!BC(!DE + DE) + DE(B!C + BC) = F … (Distributive Law)

!BCE + BDE = F … (Redundance Law)

which leads us to our final solution.

Simplified logic derived to filter list using Boolean algebra

This is a small example but shows how you can solve really complex logic problems in programming using Boolean algebra. Let’s see if you can solve this if we add one more criteria, priority. Post your answers in the comment section.

Go-Figure: To-Do list with Priority
To-Do App with Filter created using Boolean Algebra

Just seeking knowledge and sharing it with others

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store