.NET Tip: Avoiding Boolean Overload

I mentor the robotics team at a local high school. Recently, I was reviewing the code of a couple students when I came across what appeared to be some very complex boolean logic. The code was using the input from several sensors to determine what to do next. Following the logic when there are only a couple booleans to take into account is not that bad, but what do you do when you are evaluating 6-8 different variables? The logic can get confusing very quickly. The first step I suggest taking is to try to simplify the boolean logic. Here are some basic rules you can apply to try to reduce your boolean logic complexity:

 1. !(!x) == x
 2. x | x == x
 3. x | !x == true
 4. !x | !y == !(x & y)  - DeMorgan's Theorem
 5. !x & !y == !(x | y)  - DeMorgan's Theorem
 6. x & x == x
 7. x & !x == false
 8. x | y == y | x  - Commutative Law
 9. x & y == y & x  - Commutative Law
10. (x | y) | z == x | (y | x)  - Associative Law
11. (x & y) & z == x & (y & z)  - Associative Law
12. x & y | x & z == x & (y | z)  - Distributive Law
13. (x | y) & (x | x) == x | (y & z)  - Distributive Law
14. x | x & y == x
15. x & y | x & !y == x
16. (x & y) | (!x & z) | (y & z) == (x & y) | (!x & z)
17. (x | y) & (!x | z) & (y | z) == (x | y) & (!x | z)
18. x & (x | y) == x
19. (x | y) & (x | !y) == x

This is by no means a comprehensive list. Applying these simple rules, however, could take logic that is nearly impossible to understand and make it comprehendible. In my case, what appeared to be very complex logic boiled down to four if statements, each of which only needed to evaluate two variables. Boolean logic isn't the only place this type of thinking can help. Any time you come across code that is hard to understand, see whether there is a means to simplify the logic or remove conditions. The code will be much easier to follow and the next person who has to maintain the application will thank you.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.



Comments

  • Karnaugh maps

    Posted by Alok Govil on 02/27/2009 01:09pm

    I rate this article excellent because I believe it touches the heart of issues. Not just when writing software, but when encountering day-to-day issues. Number of variables involved in daily life is quite high and even technical people often use logic incorrectly. ----------- I find it a lot easier to use Karnaugh maps to simplify such expressions. They also help in reducing nested ifs and elses. ----------- I agree that simplifying the logical expression this way can make it appear smaller to look at but more complicated to follow. On the other hand, this simplification exercise always helps you learn the true relationships between the variables involved, see what matters and what doesn't, even in cases where the smaller expression becomes more difficult to follow. The latter then I believe is only because the resulting expression is counter-intuitive, which then is only because the persons intuition was not telling him/her the right thing! ----------- After going though the simplification exercise, I then rework how much I should bundle into a single condition expression and how much I should make into nested if-else statements. Sometimes my nested statements are redundant for the compiler since nesting was done only for the purpose of writing comments!!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds