# Source Code Refactoring

If you are a software engineer, you can use Gorgeous Karnaugh to refactor your existing source code and minimize logical conditions in conditional and loop statements.

Regardless of what programming language you use, every program has a number of conditional parts, and the execution flow is controlled by conditional and loop operators. A conditional operator tests the expression passed to it as an argument and executes one of the two branches of code, depending on whether the logical expression is True or False. We do not recommend overloading your code with cumbersome conditional expressions: It will be harder to read, and more error-prone too.

## Using the Formal Method when Writing Conditional Statements

When calculated, a conditional expression produces a result: True or False. Each expression consists of logical variables (flags), which may be Boolean ones or the result of executing comparison operators or other operators. Let’s apply the formal method to minimizing a conditional statement written in C:

// ... if ( (!x1 && !x2 && !x3 && !x4) || (!x1 && !x2 && x3 && !x4) || (x2 && !x3 && x4) || (!x1 && x2 && x3 && x4) || (!x1 && x2 && !x4) || (x1 && x2 && x3 && x4) || (x1 && !x3 && x4) || (x1 && !x2 && x3 && !x4) ) { } // ...

Write your analytical statement for minimization (the && an || double symbols are supported, as well as the & and | single symbols):

F = (!x1 && !x2 && !x3 && !x4) || (!x1 && !x2 && x3 && !x4) || (x2 && !x3 && x4) || (!x1 && x2 && x3 && x4) || (!x1 && x2 && !x4) || (x1 && x2 && x3 && x4) || (x1 && !x3 && x4) || (x1 && !x2 && x3 && !x4);

Launch Gorgeous Karnaugh, select “File” — “New” — “From analytic form” in the menu, and enter the above logical function. After entering the expression, select “Minimize all with Espresso” in the menu, and choose the least “expensive” result (by 1 or by 0). Check the resulting expression in the “Minimization results” window on the C/C++ tab and copy the resulting code for inserting into your program:

// ... /* F */ if (x2&&x4 || !x1&&!x4 || x1&&!x3&&x4 || !x2&&x3&&!x4) { /* true branch */ } else { /* false branch */ } // ...

## Using Expressions in Conditional Operators

Conditional operators often include not just simple Boolean flags but various expressions with relational operators. If you define such expressions explicitely, they will be inserted into the resulting expression when the C code is generated:

$SUBSTEXPR x1 = i!=End; $SUBSTEXPR x2 = i>A; $SUBSTEXPR x3 = j==Begin; $SUBSTEXPR x4 = j<=B; F = (!x1 && !x2 && !x3 && !x4) || (!x1 && !x2 && x3 && !x4) || (x2 && !x3 && x4) || (!x1 && x2 && x3 && x4) || (!x1 && x2 && !x4) || (x1 && x2 && x3 && x4) || (x1 && !x3 && x4) || (x1 && !x2 && x3 && !x4);

After the above function is processed, the following C expression will be generated:

/* F */ if ((i>A)&&(j<=B) || !(i!=End)&&!(j<=B) || (i!=End)&&!(j==Begin)&&(j<=B) || !(i>A)&&(j==Begin)&&!(j<=B)) { /* true branch */ } else { /* false branch */ }

## Gorgeous Karnaugh for Different Programming Languages

As we have demonstrated, Gorgeous Karnaugh can effectively minimize C/C++ conditional expressions. The syntax in C#, Objective-C, Java, JavaScript, PHP, and Perl is similar to that in C++, so our software can easily minimize expressions in these programming languages too. Gorgeous Karnaugh also supports languages with a different syntax of conditional expressions, such as Pascal, Lua, Ruby, and Python.

## Gorgeous Karnaugh: The Version for Software Engineers

If you a programmer, we recommend using Gorgeous Karnaugh Standard.

Download Gorgeous Karnaugh Standard.

Purchase Gorgeous Karnaugh Standard.