Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 13 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
13
Dung lượng
653,97 KB
Nội dung
Coding Lecturer: N Huy B nhbien@fit.hcmuns.edu.vn Books AndReading There is no course textbook. Here are some useful books: • Code Complete, Second Edition by Steve McConnell, 2004 Structured 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 Variable temp = 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. Live 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 Deep 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 Complexity Make things as simple as possiblebut 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. 05 The routine is probably fine. 610 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. Boolean 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) Routine How Long Can a Routine Be? 200 How to Use Routine Parameters? input-modify-output Pseudocode Programming Process - PPP Test-Driven Development TDD Direct 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 [...]... 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 Stair-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 Range Grade >= 9 A . Coding Lecturer: N Huy B nhbien@fit.hcmuns.edu.vn Books And Reading There is no course textbook. Here are some useful books: • Code Complete, Second Edition by Steve McConnell, 20 04 Structured. 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. 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 >