Sorce code refactoring with Karnaugh Minimizer  

   Every program has conditional parts, no matter which language you are using
C#, C++, Basic, Pascal or others, they can be small or huge, the point is
that they can produce only two answers - (True or False).
Each IF (Condition) … THEN … ELSE construction or alternative ones where
Boolean function are used as routing factor are not optimized at all!
But they can be optimized!

   Yep, sure, you can object to this statement by saying: "Modern compilers
are smart enough to do this work for me…" so it looks like you haven't
anything to worry about, yeah ?

   Since you don't care what optimization steps was performed by your compiler,
you still have to deal with this huge conditional equations which resides between
IF (…) parentheses all over your source code, definitely each huge IF (…) condition
reduce readability and quality of your source code, so what we can do in order to fix
the situation ? How we can improve readability and increase quality of your existing
source code?

   Boolean minimization - that is the key answer to this problem,
Karnaugh Minimizer Standard can help you to simplify logical equations all over
your source code, and you can use this tool as a little helper in your hard
refactoring works.

   For example we can illustrate some cases for which Boolean minimization is
required, we would use Object Pascal language to show such situations in great details,
lets imagine that we have this piece of code:

if (not Flag1 and not Flag2 and not Flag3 and not Flag4)
    or (not Flag1 and not Flag2 and Flag3 and not Flag4)
    or (not Flag1 and Flag2 and not Flag3 and Flag4)
    or (not Flag1 and Flag2 and Flag3 and Flag4)
    or (not Flag1 and Flag2 and Flag3 and not Flag4)
    or (Flag1 and Flag2 and Flag3 and Flag4)
    or (Flag1 and not Flag2 and not Flag3 and Flag4)
    or (Flag1 and not Flag2 and Flag3 and not Flag4) then

   All FlagX variables (Flag1, Flag2, Flag3, Flag4) as of Boolean data type for short, now
we'll try to do something with this code, first of all we need to substitute FlagX variables
with simple alphabets, Flag1 = A, Flag2 = B, and so on, and = "*", or = "+", not = "|".
We would get following Boolean equation:

|A*|B*|C*|D + |A*|B*C*|D + |A*B*|C*D + |A*B*C*D + |A*B*C*|D + A*B*C*D + A*|B*|C*D + A*|B*C*|D

Now it is the right time to use our little helper, run Karnaugh Minimizer, press "Formula"
button from tools panel, copy & paste our above equation into this form and press "Ok",
Karnaugh Map for this equation is ready for optimization.

Karnaugh map for our equation

Press "Analyze" button that is it, now we have optimized results right to the map, results
are shown as tree of terms, root node would have this equation:
|B*C*|D + B*C*D + |A*B*C + |A*B*D + |A*|B*|D + A*|B*|C*D

   In order to optimize the results even more, we can call popup menu on that root node
and choose "Copy optimized" menu item.

After future optimizations whole equation will looks like:
C*(B |@ D)+|A*B*(C+D)+|B*(|A*|D+A*|C*D)

Note: "|@" symbol denote XNOR operator, and you can revert B |@ D fragment
as |B*|D + B*D or it would be (not Flag2 and not Flag4 or Flag2 and Flag4) in Object Pascal.

   And this equation is located in clipboard! Paste it to the source code editor and after
you have all symbols reverted back we'll get this piece of code:

if Flag3 and (not Flag2 and not Flag4 or Flag2 and Flag4)
    or not Flag1 and Flag2 and (Flag3 or Flag4)
    or not Flag2 and (not Flag1 and not Flag4
    or Flag1 and not Flag3 and Flag4) then

   As we can see the resulted piece of source code is twice as short as its initial
representation, Nice, Isn't it? Now we have neat 4 lines condition instead of 8
lines, that is better than leave all as it is and bypass this problem.

   Of course such a complicated conditions as discussed above is very rarely takes
place in the real source code, but it is the way to show, how you can perform
low cost and high quality refactoring using pure logic by yourself.

Home | About | Download | FAQ | Clients
History | Links | Link to us | Purchase | Get it Free | Screen shots

Project powered by Pigleon mailer
© Copyright 2002 -  ShurikSoft. All rights reserved.