Avoiding Boolean Overload

Reduce your boolean logic to keep you program readable. Read this article on and learn more.

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



Read the Rest of this Article at Developer.com

Related Articles
- An Active Server Pages Tutorial, Part 2
We conclude our tutorial on working with ASP by looking at Boolean functions, sessions and cookies, text files, and accessing databases.
- Applied Microsoft .NET Framework Programming
- Capturing CommandBar Events in VB Add-Ins
Regional Articles
- Avoiding Boolean Overload Alabama
- Avoiding Boolean Overload Alaska
- Avoiding Boolean Overload Arizona
- Avoiding Boolean Overload Arkansas
- Avoiding Boolean Overload California
- Avoiding Boolean Overload Colorado
- Avoiding Boolean Overload Connecticut
- Avoiding Boolean Overload DC
- Avoiding Boolean Overload Delaware
- Avoiding Boolean Overload Florida
- Avoiding Boolean Overload Georgia
- Avoiding Boolean Overload Hawaii
- Avoiding Boolean Overload Idaho
- Avoiding Boolean Overload Illinois
- Avoiding Boolean Overload Indiana
- Avoiding Boolean Overload Iowa
- Avoiding Boolean Overload Kansas
- Avoiding Boolean Overload Kentucky
- Avoiding Boolean Overload Louisiana
- Avoiding Boolean Overload Maine
- Avoiding Boolean Overload Maryland
- Avoiding Boolean Overload Massachusetts
- Avoiding Boolean Overload Michigan
- Avoiding Boolean Overload Minnesota
- Avoiding Boolean Overload Mississippi
- Avoiding Boolean Overload Missouri
- Avoiding Boolean Overload Montana
- Avoiding Boolean Overload Nebraska
- Avoiding Boolean Overload Nevada
- Avoiding Boolean Overload New Hampshire
- Avoiding Boolean Overload New Jersey
- Avoiding Boolean Overload New Mexico
- Avoiding Boolean Overload New York
- Avoiding Boolean Overload North Carolina
- Avoiding Boolean Overload North Dakota
- Avoiding Boolean Overload Ohio
- Avoiding Boolean Overload Oklahoma
- Avoiding Boolean Overload Oregon
- Avoiding Boolean Overload Pennsylvania
- Avoiding Boolean Overload Rhode Island
- Avoiding Boolean Overload South Carolina
- Avoiding Boolean Overload South Dakota
- Avoiding Boolean Overload Tennessee
- Avoiding Boolean Overload Texas
- Avoiding Boolean Overload Utah
- Avoiding Boolean Overload Vermont
- Avoiding Boolean Overload Virginia
- Avoiding Boolean Overload Washington
- Avoiding Boolean Overload West Virginia
- Avoiding Boolean Overload Wisconsin
- Avoiding Boolean Overload Wyoming
Related Articles
- An Active Server Pages Tutorial, Part 2
We conclude our tutorial on working with ASP by looking at Boolean functions, sessions and cookies, text files, and accessing databases.
- Applied Microsoft .NET Framework Programming
- Capturing CommandBar Events in VB Add-Ins

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML