Software Engineering: Rebuild and Simplify Complex Decision Blocks - Part 2


This three-part article shows how to rebuild and simplify complex if-then-else decision blocks, using string variables and switch (or case) statements. It will show how the technique works with sample applications built with Google Apps Script, C#, and This technique should work in any language that handles string variables, switch statements, and boolean conditions.

Part one described the problem to solve, the technique to solve it, a sample Google Apps Script to show the solution in action, and the engineering behind that solution.

Part two here will explain the C# solution and its engineering.

Part three will describe the solution and its engineering.

Download all the sample software behind this article - Google Apps Script, C#, and - from the GitHub repository.

The Windows desktop C# solution essentially clones the Google Apps Script solution of part one, adding a CLOSE button

1. C# application - initial state
to close the application. It has the same

2. C# application in use
user experience.

The Form1.cs submit button click method at line 92

3. Submit button click method
looks and works almost exactly like the submitData() function described in part oneStarting at line 94, the method declares and initializes four string variables, all of which will potentially become part of the assembled results message. The first four will dynamically match the potential user form picks. The fifth, assembledFlagFormString, will hold the assembled results message string the method will place on the form.

Line 114 declares string variable flagString, which will hold the concatenated boolean values that map to the user responses.

Lines 108 to 111 together declare four string indicator variables. Each maps to the user response at a specific page control. The nested functions map the relevant response values to "0" or "1" string values, so they work as booleans. Lines 118 to 121 map the relevant page control states to the boolean indicator flag variables. Line 134 concatenates the indicators into an assembled value for the flagString variable. Because of this, flagString essentially becomes a base-two integer.

At line 136, submitData() uses flagString in the switch statement to build out the assembledFlagFormString variable, with strings mapped to the user form picks. This part also works exactly like the submitData() switch statement described in part one, and it includes all the advantages. At the end of the method, line 221 places the variable value in control RTB2 (Rich Textbox 2).

Method resetControls() at line 247

4. The resetControls() method
resets all controls and removes the user response table from the page.

Groups of "similar" form controls have identical behavior. For example, checkboxes in the Launch Systems group will have the same check changed behavior. This means that ideally, these controls would each have the same check changed event

5. The check changed method
handler, to simplify the application. C# event handlers make this possible. In Form1.Designer.cs

6. Use the same event handler for different controls
I tied the CB_CheckedChanged method to the checkbox controls using the System.EventHandler statements. These statements point to the CB_CheckedChanged method in Form1.cs. I also used this technique for the radio controls (RB1 to RB5) in the Interplanetary Mission groupbox.

Part 2 here showed how the C# demo application works. Part 3 will look at the version.