Structured ProgrammingThe core of structured programming is the simple idea that a program should use only one-in, one-out control constructs.. Coding time use of magic numbers 2.. Load
Trang 1nhbien@fit.hcmuns.edu.vn
Trang 2Books And Reading
There is no course textbook Here are some useful books:
• Code Complete, Second Edition by Steve McConnell, 2004
Trang 3Structured Programming
The core of structured programming is the simple idea that a program should use only one-in, one-out control constructs
Selection
Iteration Sequence
Trang 4temp = Sqrt( b*b - 4*a*c );
root[0] = ( -b + temp ) / ( 2 * a );
root[1] = ( -b - temp ) / ( 2 * a );
// swap the roots
temp = root[0];
root[0] = root[1];
root[1] = temp;
Binding Time
1 Coding time (use of magic numbers)
2 Compile time (use of a named constant)
3 Load time (reading a value from an external source such as the Windows registry file or a Java properties file at program load time)
4 Object instantiation time (such as reading the value each time a window is created)
5 Just in time (such as reading the value each time the window is drawn)
Using Each Variable for Exactly One
Purpose
The earlier the binding time, the lower the flexibility and the lower the complexity
Trang 5Live Time
Keep Variables "Live" for as Short a Time as Possible
1 // initialize all variables
2 recordIndex = 0;
3 total = 0;
4 done = false;
26 while ( recordIndex < recordCount ) {
27
28 recordIndex = recordIndex + 1; < 1
64 while ( !done ) {
69 if ( total > projectedTotal ) { < 2
70 done = true; < 3
recordIndex
( line 28 - line 2 + 1 ) = 27
total
( line 69 - line 3 + 1 ) = 67
done
( line 70 - line 4 + 1 ) = 67 Average Live Time
( 27 + 67 + 67 ) / 3 = 54
Trang 6Deep Nesting
Avoiding nesting to more than three or four levels
if ( inputStatus == InputStatus_Success ) {
// lots of code
if ( printerRoutine != NULL ) {
// lots of code
if ( SetupPage() ) {
// lots of code
if ( AllocMem( &printData ) ) {
// lots of code
}
}
}
}
Retesting
Return
if-then-elses
case
GoTo
Routines
Polymorphism
Exceptions
Redesign
Trang 7Make things as simple as possible but no simpler
Albert Einstein
if ( ( (status = Success) and done ) or
( not done and ( numLines >= maxLines
) ) ) then
1 Start with 1 for the straight path through the routine
2 Add 1 for each of the following
keywords, or their equivalents: if
while repeat for and or
3 Add 1 for each case in a case
statement
0 5
The routine is probably fine
6 10
Start to think about ways to simplify the
routine
10+
Break part of the routine into a second
routine and call it from the first routine
Trang 8Boolean Expressions
if (!statusOK) {
// do something
}
else {
// do something else
}
if ( !displayOK || !printerOK )
while ( i < MAX_ELEMENTS and item[ i ] <> 0 )
if ( ( ( item / denominator ) > MIN_VALUE ) && ( denominator != 0 ) )
if ( i > MIN_ELEMENTS ) and ( i < MAX_ELEMENTS )
if (i < MIN_ELEMENTS or i > MAX_ELEMENTS)
Trang 9How Long Can a Routine Be?
200
How to Use Routine Parameters? input-modify-output
Pseudocode Programming Process
- PPP
Test-Driven Development TDD
Trang 10Direct Access Tables
Insurance Rates (Gender, Marital Status,
Smoking Status, Age)
if ( gender == Gender.Female ) {
if ( maritalStatus == MaritalStatus.Single ) {
if ( smokingStatus ==
SmokingStatus.NonSmoking ) {
if ( age < 18 ) { rate = 200.00;}
else if ( age == 18 ) {rate = 250.00;}
else if ( age == 19 ) {rate = 300.00;}
A table-driven method is a scheme that allows
you to look up information in a table rather
than using logic statements (if and case) to
figure it out
Day-In-Month
If ( month = 1 ) Then days = 31
ElseIf ( month = 2 ) Then days = 28
ElseIf ( month = 3 ) Then days = 31
Trang 11Indexed Access Tables
Rather than being accessed directly, an indexed access table is accessed via an intermediate index
9999
0000
When you use indexes, you use the primary data to look up a key in an index table and then you use the value from the index table to look up the main data you're interested in
1
100
Trang 12Stair-Step Access Tables
The stair-step approach categorizes each entry by determining the level at which
it hits a "staircase." The "step" it hits determines its category
< 8.5 C
< 7.5 E
To use the stair-step method, you put the upper end of each range into a table and then write a loop to check a score against the upper end of each range When you find the point at which the score first exceeds the top of a range, you know what the grade is
Trang 13Thank you for your time