Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simplifying Flow of Control

Similar presentations


Presentation on theme: "Simplifying Flow of Control"— Presentation transcript:

1 Simplifying Flow of Control
Table Driven Logic Simplifying Flow of Control Largely extracted from Code Complete by Steve McConnell Copyright © 2016 Curt Hill

2 Introduction The idea here is to consider a number of table techniques to reduce the programming logic of a module The table lookup will replace a number of if and/or switch statements If we can apply these ideas it will make our code simpler and easier to understand Usually faster as well Copyright © 2016 Curt Hill

3 Roots Prior to your existence the IBM 360 had a translate instruction
It was used to translate one character set into another, among other things What you had was a table of 256 characters You used a character as a subscript into this table It then found the corresponding character Copyright © 2016 Curt Hill

4 Example IBM 360s used EBCDIC, but they often had to deal with ASCII characters from other machines Suppose we have an ASCII file Load a table with EBCDIC characters in their ASCII positions Then use each ASCII character as a subscript into this table The file is then translated from ASCII to EBCDIC Copyright © 2016 Curt Hill

5 Parts of this table 32 blank 33 ! 34 “ 35 # 36 $ 37 % 38 & 46 . 47 /
Position Character 32 blank 33 ! 34 35 # 36 $ 37 % 38 & Position Character 46 . 47 / 48 49 1 50 2 51 3 52 4 Position Character 60 < 61 * 62 > 63 ? 64 @ 65 A 66 B Table would only need to be 128 characters ASCII blank is 32, EBCDIC blank is 64 Copyright © 2016 Curt Hill

6 Continuing The IBM 360 is largely dead and gone except for museum pieces The technique continues Copyright © 2016 Curt Hill

7 Issues We need to be concerned about two issues
How to look up entries? What should be stored in the table? Then we can consider three approaches Direct access Indirect access Stair-step access Then we will know it all We hope Copyright © 2016 Curt Hill

8 Direct Access Tables A direct access table allows a direct lookup from part or all of the data The IBM 360 character translate is an example Typically we have an integral item and we can adjust its range into the subscript Looking up month names is also a simple example Copyright © 2016 Curt Hill

9 Month Name Lookup In any of many Date objects you saw code like this: switch(month){ case 1: name = “January”; break; case 2: name = “Febuary”; break; case 3: name = “March”; break; … } This works but what does it do to the complexity and LOC? Even uglier than nested ifs Copyright © 2016 Curt Hill

10 Month Name Lookup Again
Instead try this: String months[] = {“January”,“Febuary”, “March”, … }; … name = months[month-1];} This is much simpler There are many other sorts of subscript adjustments Such as this month-1 Copyright © 2016 Curt Hill

11 Number of Days This could get somewhat more complicated if we wanted number of days in a month We would have a table of numbers We would need one if following the assignment that checked if it was February and a leap year This would still be easier than the switch seen in the Date class Copyright © 2016 Curt Hill

12 Dimensionality There is no reason why the table has to be a single dimension We may end up with an array of several dimensions We may use several types of variables and adjust each one in a separate way Copyright © 2016 Curt Hill

13 Insurance Rates Suppose we want to lookup driver’s insurance rates
The factors that we might consider would be: Age Gender Marital status The lookup could be a complicated collection of decision statements or: rate = rats[age][gender][ms]; Copyright © 2016 Curt Hill

14 Practicalities This one would be better read in at program startup
This allows rates to change over time Whether gender and marital status are enumerations or Booleans is irrelevant Both map into zero based values The lowest driving age may need to be subtracted from the age of the client to make that zero based Copyright © 2016 Curt Hill

15 Lookup Keys with Issues
There are many potential keys that do not map onto zero based integers very well In the insurance rate example we would have to eliminate ages less than 16 (or so) We also want to collapse ranges of rates There is not much difference between a driver of age 34 and one of age 35 In these cases we use a function to transform the key This key transformation is not hashing although it sounds like it in some ways Copyright © 2016 Curt Hill

16 Indexed Access Tables Often it is difficult to do the transformation
It is not a case of simple mathematical operations such as subtraction and division In these situations we use two tables The first is the table of indices The second is the table of data Particularly good when the data is much larger than the index Copyright © 2016 Curt Hill

17 Indexed Picture Index table Data table Usually close to full
Could be quite sparse Copyright © 2016 Curt Hill

18 Commentary Although you could use a hash in this case this not usually the idea What you usually have is numeric key but it is sparse Consider products that have a four digit product number, but there are currently only 200 of them We do not mind wasting space in the index table, but a data table entry consumes a lot more space The index table could be pointers as well Copyright © 2016 Curt Hill

19 VLookup Recall the VLookup function of Excel
This is a rectangle of two rows and several columns We take a value and find its row in the first column and then use the value in the second column This is routinely how a letter grade is computed in spreadsheets Copyright © 2016 Curt Hill

20 Grades with Decisions You probably remember something like this: if(grade< .6) s = “F”; else if(grade < .7) s = “D”; else if(grade < .6) … We can do this with a Stair-Step Table Copyright © 2016 Curt Hill

21 Stair-Step Tables The idea is a two column array
First column has the maximum (or minimum) value of that range Second column the lookup value The ranges do not have to be equal sized If we read the data from disk, we can change sizes without changing the code Copyright © 2016 Curt Hill

22 Example typedef struct { float value; BigClass data;} entry;
entry table[50]; int size; float val; BigClass result; for(int i = 0;i<size;i++){ if(val < table[i].value){ result = table[i].data; break; } Copyright © 2016 Curt Hill

23 Finally Table driven methods can increase flexibility, while reducing code The trick is to find the right type of table and the right type of initialization Copyright © 2016 Curt Hill


Download ppt "Simplifying Flow of Control"

Similar presentations


Ads by Google