2
3
Weinberg LOC 1 4 2 5 5 1 5 2-3 3 2 3 4 2 5 1 2-3 4 3 4 1 1: 5: 4
- - Documentation vs. 5
1 ( ), 2 FORTRAN, COBOL, ALGOL60, BASIC 3 : Pascal, Modula-2, C, Ada, C++, Java : CHILL, RPG, Lisp, Prolog, APL 4 (4GL) non-procedural language MANTIS, IDEAL, RAMIS II, SQL 6
Visual Basic, (properties) 7
( ) 8
9
->,,,,, 10
? < > COBOL : C : Pascal : ( ) Modula-2 : Ada : LISP, Prolog :,, <> COBOL-74: 300, COBOL-85: 74 Pascal: 74, Modula-2: 64, Fortran 48 (orthogonality): 11
<> Pascal if TotalSales > BonusLevel then Bonus := Commission + (BonusPercent * TotalSales); if TotalSales > BonusLevel then begin end Bonus := Commission + (BonusPercent * TotalSales); BonusMonths := BonusMonths + 1 Ada endif, endwhile if TotalSales > BonusLevel then endif Bonus := Commission + (BonusPercent * TotalSales) if TotalSales > BonusLevel then endif Bonus := Commission + (BonusPercent * TotalSales); BonusMonths := BonusMonths + 1 12
( ) dangling else if conditiona then if concitionb then action1 else action2 1: null statement if conditiona then if concitionb then action1 else {} else action2 2: begin-end if conditiona then begin if concitionb then action1 end else action2 3: if conditiona then if concitionb then action1 endif else action2 endif 13
( ) (readability) (clarity) FORTRAN, COBOL )FORTRAN1~5, 7~72 FORTRAN FORTRAN, BASIC 14
(case ) case case case Person of when Newborn Infant => Infant_Seat; when Toddler.. Child => Lap_Belt; when Others => Shoulder_and_Lap_Belt; end case;,, 15
strongly typed language Ada, Pascal, C++ dynamic type language Lisp, APL,,, (C ) enum people {Infant, Toddler, Preschool, Child, Teenager, Adult}; struct student {char* name; int st_id; char* dept;}; ADT ( ) class 16
FORTRAN, Pascal, Modula-2 C, Ada < > type Puzzle is array (integer range <>, integer range <>) of character; (structure) subtype SundayPuzzle is Puzzle(1..50, 1..50); <> type SubscriberType = record Name: array[1..50] of char; IDNumber: 10000..99999; IssuesSent: 0..104; IssuesRemaining: 0..104; SubscriptionType: (New, Renewal, Free, Lifetime) end; 17
const (,,, ) (Pascal, Modula-2) (Ada) #define (C) (C++) 18
(Ada package) generic type StackItem is private; package Stack is procedure Push(Element: in StackItem); function Pop return StackItem;.. end Stack; package CharStack is new Stack(character); package IntegerStack is new Stack(integer); 19
(C++ Class) class Employee { private: char* positiontype int... public: promote(... ); } name; position; salary; Employee YoungHee(); Employee* YoungHee = Employee(); 20
(formulation) - N. Wirth - E. Yourdon 21
Proper Program, (,, ) goto stepwise refinement Structure Theorem proper program (if-then, if-then-else, case, while, for, repeat-until ) 22
23
24
Goto DO 50 I=1, COUNT. IF (ERROR1) GO TO 60. IF (ERROR2) GO TO 70. 50 CONTINUE 60 {Code for Error1 handling} GO TO 80 70 {Code for Error handling} 80 CONTINUE I=1 fori=1totablesizedo while I <= TableSize and if Table(I) = Target then goto Found Table(I) <> Target do I = I + 1 NotFound: {code for Target not found} if I > TableSize then Found: {code for Target found} {code for target not found} else { code for Target found} (b) goto (a) 25
1: - :? int i, j; for( i = 0; i < N; i++) { float v[n][n]; for(j = 0; j < N; j++) {. v[i][j] = 0.0; for(i = 1; i <= N; i++) } for( j = 1; j <= N; j++) v[i][j] = 1.0; v[i-1][j-1] = ( i / j )*( j / i ); } void FillPowersArray(int base, int *powers) { } int i; *powers = base; for (i = 2; i <= 8; i++) *powers+i = *power+i-1 * base; 26
( ) 2: < > < > if ( x < y ) { small = x; if ( x < z ) small = x; if ( y < small ) small = y; if ( x > z ) small = z; if ( z < small ) small = z; } if ( x > y ) { if ( y < z ) small = y; if ( y > z ) small = z; } If... then... else < > < > if (in_user_code) { if (!in_user_code) revert(); in_user_code = FALSE; else { r2 = r; reset_pharlap(); in_user_code = FALSE; r2 = r; send_sig_segv(); reset_pharlap(); } send_sig_segv(); else revert(); } 27
( ) 3: < > < > t1 = x1 - ( x2 + x2 ); t2 = 7 - x2; y = t1 + t1 + t2 + t2; y = 2*( x2-2 * x2 ) + 2 * (7 - x2); 4: < > NO5S = NO5S + 1; n = k; nn = k * k; nnn = k * k * k; <> -1 l a1 al -0 O term0 termo -5 S TEXT5 TEXTS -I l Iist list -mn mnnm mnmn -uv vuvu uuvu 28
( ) Hungarian notation < > char buffer[500], mssge[80]; void read_instnance(void), SaveCurrent(void); void get_line(void), write_line(void); int index1, index2; int dirctry, vsble; < > char buffer[500], message[80]; void read_instnance(void), save_current(void); void read_line(void), write_line(void); int i, j; int drctry, vsble; 29
( ) 6: { } < > < > for(i= 0; i <N; i++) for ( i = 0; i < N; i++ ) { { k= a[i]; k = a[i]; if (k>100) a[i] =i*3; if ( k > 100 ) else if (j =N) a[i] = i * 3;... else if ( j = N) } }... 7: If-else. < > < > if (swctl == '1') goto conti; if (swctl!= '1') { else { divctl += 10; dvictl += 10; swctl = '1'; swctl = '1'; } } conti : 30
( ) 8: if if null else < > if ( qty > 10 ) /* A */ if ( qty > 200 ) /* B */ if ( qty >= 500 ) bill_a += 1.00; /* C */ else bill_a += 0.50; /* C */ else; /* B */ else bill_a = 0.0; /* A */ if ( qty >= 500 ) bill_a += 1.0; else if ( qty > 200 ) bill_a += 0.5; else if ( qty <= 10) bill_a = 0.0; < > if ( qty >= 500.0 ) bill_a += 1.0; if ( qty < 500 && qty > 200 ) bill_a += 0.5; if ( qty <= 10 ) bill_a = 0.0; 31
( ) 9:. < > < get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); while (ntokens < T_LIMIT) { process_token(); add_token(); get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); } > for ( ; ; ) { } get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); if (T_LIMIT <= ntokens) break; process_token(); add_entry(); 32
( ) 10:, < > search(board,i,j,movrow,movcol,l) intboard[8][8],i,j,movrow[4],movcol[4],*l; { /*. 1 -, 2 -, 3 -, 4-0,. movrow, movcol l. */ int k; if (board[i][j] == 0) exit(0); *l = 0; k = board[i][j]; if (k == 3) goto back_right; if (i == 7) goto exit_test; if (j == 7) goto for_left; if (board[i+1][j+1]!= 0) goto for_left; moverow[*1] = i + 1; movecol[*l] = j + 1; *l++; for_left: if (j == 0) goto exit_test; moverow[*l] = i+1; movecol[*l] = j-1; *l++; exit_test: if (k == l) return; back_right: if (i == 0) return; if (j == 7) goto back_left; if (board[i-1][j+1]!= 0) goto back_left; moverow[*l] = i-1; movecol[*l] = j+1; *l++; back_left: if (j == 0) return; if (board[i-1][j-1]!= 0) return; moverow[*l] = i-1; movecol[*l] = j-1; *l++; } 33
( ) < > search(board, i, j, row, col, l) int board[8][8], i, j, row[4], col[4], *l; { /* board[i][j] = 0 => 1 =>, 2=> 3 =>, 4=> i, j l row, col */ int k; if (i < 0 i > 7 j < 0 j > 7) error("out of board"); k = board[i][j]; *l = 0; if (k < l k >4) error("misplaced knight"); *l = 0; if (k!= 3) store(board, i+1, j+1, row, col, l); if (k!= 3) store(board, i+1, j-1, row, col, l); if (k!= 1) store(board, i-1, j+1, row, col, l); if (k!= 1) store(board, i-1, j-1, row, col, l); } void store(board, ic, jc, row, col, l) int board[8][8], ic, jc, row[4], col[4], *l; { if (ic < 0 ic > 7 }} jc < 0 jc > 7) return; if (board[ic][jc]!= 0) return; *l++; row[*l] = ic; col[*l] = jc; } 34
-.. 35
( )... -. (off-by-one)...... -.. -. 36
The sooner you start to code, the longer the program will take. If you can't write it down in English, you can't code it. If the code and the comments disagree, then both are probably wrong. If you have too many special cases, you are doing it wrong. Get your data structures correct first, and the rest of the program will write itself. Don't debug standing up. It cuts your patience in half, and you need all you can muster. Each new user of a new system uncovers a new class bugs. Whenever possible, steal code. Always do the hard part first. If the hard part is impossible, why waste time on the easy part? If you lie to the computer, it will get you. One person's constant is another person's variable. 37
38
/********************************************************************************************* - EXAM SCORE SUMMARY PROGRAM - WRITTEN BY : HONG, KIL DONG - RELEASE DATE : April 1, 1994 - - PURPOSE : This program produces a variety of statistics on a - group of student's exam scores. For each score that is entered, - the score and a row of stars representing the magnitute of the - score is printed out. In addition, the number of scores, the - average score, the highest score and the lowest score are - printed out. - - DESIGN : The program is composed of the following modules: - Main module - The Main, controlling module - Validate Input - Gets and validates the user's input - Process Valid Score - Prints score and row of stars for one score - Update Statistics - Updates global statistics - Print Summary Report - Prints summary statistics 39
( ) - MAJOR VARIABLES : - SumOfScore (subrange of integer, 0..2000) - - The sum of all score entered - NumOfStudents (subrange of integer, 0..100) - - The total number of score entered - LargestScore (subrange of integer, 0..20) - - The largest score in the set of scores - SmallestScore (subrange of integer, 0..20) - - The smallest score - FILES USED: - Input - contains integer scores, one to a line - Output - contains a one-line histogram for each score - read, followed on the next line by the number of - students and their average score, then on the next line - by the top score, and concluding on the next line with - the lowest score. - EXTERNAL REFERENCES: None ******************************************************************************************/ 40