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

______________________

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 VB.net. 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 explained the C# solution and its engineering.

Part three here will describe the VB.net solution and its engineering.

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

The Windows desktop VB.net solution essentially clones the Google Apps Script solution of part one, adding a CLOSE button


1. VB.net application - initial state
to close the application. It has the same


2. VB.net application in use
user experience.

The submit button click method at line 106


3. Submit button click method
looks and works almost exactly like the submit button click method described in part twoStarting at line 108, 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 128 declares string variable flagString, which will hold the concatenated boolean values that map to the user responses.

Lines 122 to 125 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 132 to 135 map the relevant page control states to the boolean indicator flag variablesLine 148 concatenates the indicators into an assembled value for the flagString variable. Because of this, flagString essentially becomes a base-two integer.

Lines 150 to 216 map an if-block to the four relevant page controls. The if-blocks use jQuery to see if the user interacted with its page control. If this interaction happened, the if-block sets the appropriate indicator variable to "1".

At line 214, 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 two and it includes all the advantages. At the end of the method, line 220 places the variable value in control RTB2 (Rich Textbox 2).

In the VB.net application, groups of "similar" form controls have identical behavior, just like those in the C# application. To simplify the code, single procedures handle events for multiple controls when possible. For example, the CB_CheckedChanged event


4. The CB_CheckedChange procedure handles the CheckedChange
event for all the Launch Systems checkboxes
at line 63 handles the CheckedChange event for all the launch system checkboxes. I also used this technique for the radio controls (RB1 to RB5) in the Interplanetary Mission groupbox.

Procedure resetControls() at line 236


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

Part 3 here showed how the VB.net demo application works. As we saw, the described technique works in three very different languages and it should work in any language that handles string variables, switch statements, and boolean conditions. With it, developers can focus on the solutions they want to build instead of dealing with nasty, nested if-statements.

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 VB.net. 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 VB.net solution and its engineering.

Download all the sample software behind this article - Google Apps Script, C#, and VB.net - 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 VB.net version.

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

______________________

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 VB.net. This technique should work in any language that handles string variables, switch statements, and boolean conditions.

Part one here will describe 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 will explain the C# solution and its engineering.

Part three will describe the VB.net solution and its engineering.

Download all the sample software behind this article - Google Apps Script, C#, and VB.net - from thGitHub repository.
________________

As we build software, sooner or later we'll have to deal with nasty conditional statements and structures. We can try a "conventional" solution with if-then-else blocks - probably nested - but we'll have a hard time trying to build, test, enhance, and maintain it. For a recent Google Apps Script project, I had to deal with a set of complex business rules. Google Apps Script uses JavaScript as the development language, so I first tried a "conventional" JavaScript approach

  if (condition1) {

  } else {

    statement 1

    if (condition 1a) {

      statement 2

    } else {

      statement 3

    }

  } else if (condition2) {

    statement 4

  } . . .

and I soon realized that it would never work because of the complexity. After some trial and error, I built a solution that first breaks out each condition test as an independent component, converting each binary test result

  0 or 1

to a string. Then, the solution concatenates these component strings to build an assembled "result string". Finally, it passes the result string to a JavaScript switch statement. This approach has important advantages. We can focus on building the boolean components as a first step. We'll have an easier time testing all those components to show that they cover all possible and necessary conditions. Even better, the assembled result string essentially becomes a base-two integer. This means that we can easily verify that the switch statement covers all the required permutations. If we start with n condition tests, we will concatenate them into a result string that will have n characters, each 0 or 1. This means that the switch statement will have 2 ^ n cases, or blocks.

The sample Google Apps Script application has one form

1. Demo application - initial state
and the user clicks / types into the controls as appropriate. When the user clicks the SUBMIT button


2. Demo application in use
the application builds a results message, placing it at the lower left. The message matches the user responses, including a message for no picked controls. Since the form has four controls, the application must handle

2 ^ 4

or 16 permutations. The RESET button deletes the results message and "zeroes out" the controls.

This Google Apps Script solution is a stand-alone application not bound to an underlying Google Drive file. It has two files. The first, Code.gs, has one function - doGet()

function doGet(){

  //  A request made to the script URL runs the doGet() function.
  //  This function launches the application - it loads the HTML
  //  file switchStatement.html in the browser . . .

  return HtmlService.createTemplateFromFile("switchStatement.html").evaluate();

}

that serves as the application entry point and it immediately shifts flow tswitchStatement.html, the "primary" application file. After this shift, the application no longer uses Code.gs. The switchStatement.html file has the HTML that writes the application in the browser. In this file, the <SCRIPT> block has JavaScript functions submitData() at line 47, the primary application machinery, and resetControls() at line 232, which resets all form controls. The application relies on jQuery libraries, referenced at lines 14 and 15.

The submitData() function runs each time the user clicks the SUBMIT button.


3. Function submitData() - string variable declarations
Starting at line 51, the function first declares and initializes five string variables, all of which will potentially become part of the HTML for the results message. The first four will dynamically match the potential user form picks. The fifth, assembledFlagHTMLString, will hold the assembled HTML string that will become the finished results message on the page.

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

Lines 68 to 71 together declare four string indicator variables, initializing all of them to "0". They will work as booleans. Each maps to the user response at a specific page control. In submitData(), these variables will only have "0" or "1" values.

Lines 73 to 112 map individual if-blocks to the four relevant page controls. The if-blocks use jQuery to see if the user interacted with its page control. If this interaction happened, the if-block sets the appropriate indicator variable to "1".

At line 120


4. The switch statement
submitData() concatenates all the boolean indicator variables into string variable overallFlagString. The concatenation order matters because the line 122 switch statement maps each case to a specific component digit (a specific character) of overallFlagString.

At line 122, submitData() uses overallFlagString in the switch statement to build out the assembledFlagHTMLString variable, with strings mapped to the user form picks. Since overallFlagString variable covers all switch cases, the switch statement will go directly to the correct block, execute the lines, and then break out of the statement. This has many advantages:
  • A more conventional if-then-else structure will probably run more than one, and possibly all, boolean condition blocks in that structure
  • Developers will have a much easier time building, testing, maintaining, and enhancing the switch statement compared to an equivalent if-then-else structure
  • Developers can easily verify that the switch statement covers all cases, because of the base-two number structure of variable overallFlagString. Using base-two numbers, simply run through every number from 0 to 2 ^ n. In contrast, it could become painful to verify that an equivalent if-then-else structure covers all possibilities. Note that the comments next to each case have the base-ten integer equivalent for that case
Line 225


5. Finish the HTML string and place it on the page.
Reset the page controls.
closes assembledFlagHTMLString with a </TABLE> flag, and line 227 uses jQuery to build an HTML table with that variable, placing that table in the finished webpage.

At line 232, function resetControls() resets all controls and removes the user response table from the page.

Part 1 here showed how the Google Apps Script demo application works. Part 2 will look at the C# version.