Thursday, July 13, 2006

 

Boolean what? Rules for Simplification

We were having a small dev meeting the other day, someone said something and I replied “…it’s a bit like applying Boolean Algebra or Karnaugh maps” which resulted in several blank looks. Thinking they had misheard (my diction is not always the clearest) I repeated the statement and gave the example of applying De Morgan’s laws. These are applied by negating the individual terms, changing the AND’s=OR’s and the OR’s=AND ‘s and then negating the whole expression e.g.

A + B = NOT ( (NOT A) . (NOT B))
A . B = NOT ((NOT A) + (NOT B)) where '.' = AND, '+' = OR

Now some of you are probably saying what does this have to do with my day-to-day job? Well, applying these simplification rules can be very useful for simplying ‘if’ logic. I’m not a big fan of those ‘if’ statements that descend 15 levels deep! In fact, I always try and simplify logic to make it more understandable (no more than 3 or 4 nestings is great). I have seen some shockers: a few years ago, one coder whose code was outside my jurisiction would mangle huge, completely unfathomable nested if’s. She just did not get why this was totally wrong. Sigh! [Note: Karnaugh maps are more usually used by electrical engineers to reduce the number of gates required to implement some specific boolean logic.]


    

Powered by Blogger