490 likes | 499 Views
Learn about the key phases of software development, including specification, design, implementation, analysis, testing, maintenance, and obsolescence. Understand the importance of preconditions and postconditions in ensuring the correctness of functions.
E N D
Software Development Phases • Specification of the task • Design of a solution • Implementation of solution • Analysis of solution • Testing and debugging • Maintenance and evolution of the system • Obsolescence
Specification • Precise description of problem • May be one of the most difficult phases
Design • Algorithm – set of instructions for solving problem • Not originally done in programming language • pseudocode • Formal modeling language (UML) • Decompose problem into smaller – more manageable subtasks – map to subprograms • Need to be as independent of each other as possible • Reuse • Modification • All domain information should be located only 1 place in the code • Each subprogram should only do 1 task • Each subprogram should be treated as a black box – specification not implementation – called procedural abstraction / information hiding
Preconditions and Postconditions • An important topic: preconditions and postconditions. • They are a method of specifying what a function accomplishes. Data Structures and Other Objects Using C++
Preconditions and Postconditions Frequently a programmer must communicate precisely whata function accomplishes, without any indication of how the function does its work. Can you think of a situation where this would occur ?
Example HERE ARE THE REQUIREMENTS FOR A FUNCTION THAT I WANT YOU TO WRITE. • You are the head of a programming team and you want one of your programmers to write a function for part of a project. I DON'T CARE WHAT METHOD THE FUNCTION USES, AS LONG AS THESE REQUIREMENTS ARE MET.
What are Preconditions and Postconditions? • One way to specify such requirements is with a pair of statements about the function. • The precondition statement indicates what must be true before the function is called. • The postcondition statement indicates what will be true when the function finishes its work.
Example void write_sqrt(double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. ...
Example • The precondition and postcondition appear as comments in your program. void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. ... }
Example • In this example, the precondition requires that x >= 0 be true whenever the function is called. void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. ... }
write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Example Which of these function calls meet the precondition ?
write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Example Which of these function calls meet the precondition ? The second and third calls are fine, since the argument is greater than or equal to zero.
write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Example Which of these function calls meet the precondition ? But the first call violates the precondition, since the argument is less than zero.
Example • The postcondition always indicates what work the function has accomplished. In this case, when the function returns the square root of xhas been written. void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. ... }
Another Example bool is_vowel( char letter ) // Precondition: letter is an uppercase or // lowercase letter (in the range 'A' ... 'Z' or 'a' ... 'z') . // Postcondition: The value returned by the // function is true if Letter is a vowel; // otherwise the value returned by the function is // false. ...
Another Example What values will be returned by these function calls ? is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' );
Another Example What values will be returned by these function calls ? true is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); false Nobody knows, because the precondition has been violated.
Another Example What values will be returned by these function calls ? is_vowel( '?' ); Violating the precondition might even crash the computer.
AT THIS POINT, MY PROGRAM CALLS YOUR FUNCTION, AND I MAKE SURE THAT THE PRECONDITION IS VALID. Always make sure the precondition is valid . . . • The programmer who calls the function is responsible for ensuring that the precondition is valid when the function is called.
THEN MY FUNCTION WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE TRUE. I GUARANTEE IT. . . . so the postcondition becomes true at the function’s end. • The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes true at the function’s end.
You The programmer who wrote that torrential function Noah A Quiz Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40-day flood or other disaster?
You The programmer who calls a function is responsible for ensuring that the precondition is valid. A Quiz Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40-day flood or other disaster?
On the other hand, careful programmers also follow these rules: • When you write a function, you should make every effort to detect when a precondition has been violated. • If you detect that a precondition has been violated, then print an error message and halt the program.
On the other hand, careful programmers also follow these rules: • When you write a function, you should make every effort to detect when a precondition has been violated. • If you detect that a precondition has been violated, then print an error message and halt the program... • ...rather than causing a disaster.
Example • The assert function (described in Section 1.1) is useful for detecting violations of a precondition. void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. { assert(x >= 0); ...
Advantages of Using Preconditions and Postconditions • Succinctly describes the behavior of a function... • ... without cluttering up your thinking with details of how the function works. • At a later point, you may reimplement the function in a new way ... • ... but programs (which only depend on the precondition/postcondition) will still work with no changes.
Summary Postcondition • The programmer who writes a function ensures that the postcondition is true when the function finishes executing. Precondition • The programmer who calls a function ensures that the precondition is valid. • The programmer who writes a function can bank on the precondition being true when the function begins execution.
C++ Features: Standard Library & Standard Namespace #include directive Similar to import directive in other languages Much more primitive Namespaces – allows limiting visibility of “names” Standard namespace – standard C++ libraries 2 choices using namespace std: Fully qualified identifier
Exception Handling Similar to Java and C#
Running Time Analysis Machine independent method of measuring run time efficiency Determine basic operation for the algorithm Expression the number of operations as a function of the data size Interested in the order of magnitude of this function – expressed using Big-O notation
Running Time Analysis Worst case – maximum number of operations Best case – minimum number of operations Average case – depends upon the underlying probability distribution
FIGURE 1-1 Gift shop and each dot representing a house The Big-O Notation • Analyze algorithm after design • Example • 50 packages delivered to 50 different houses • 50 houses one mile apart, in the same area
FIGURE 1-2 Package delivering scheme The Big-O Notation • Example (cont’d.) • Driver picks up all 50 packages • Drives one mile to first house, delivers first package • Drives another mile, delivers second package • Drives another mile, delivers third package, and so on • Distance driven to deliver packages • 1+1+1+… +1 = 50 miles • Total distance traveled: 50 + 50 = 100 miles
FIGURE 1-3 Another package delivery scheme The Big-O Notation • Example (cont’d.) • Similar route to deliver another set of 50 packages • Driver picks up first package, drives one mile to the first house, delivers package, returns to the shop • Driver picks up second package, drives two miles, delivers second package, returns to the shop • Total distance traveled • 2 * (1+2+3+…+50) = 2550 miles
The Big-O Notation • Example (cont’d.) • n packages to deliver to n houses, each one mile apart • First scheme: total distance traveled • 1+1+1+… +n = 2n miles • Function of n • Second scheme: total distance traveled • 2 * (1+2+3+…+n) = 2*(n(n+1) / 2) = n2+n • Function of n2
TABLE 1-1 Various values of n, 2n, n2, and n2 + n The Big-O Notation (n) and (2n) are close, so we magnify (n) (n2) and (n2 + n) are close, so we magnify (n2)
TABLE 1-1 Various values of n, 2n, n2, and n2 + n The Big-O Notation When n becomes too large, n and n2 becomes very different
The Big-O Notation • Analyzing an algorithm • Count number of operations performed • Not affected by computer speed
The Big-O Notation Example 1-1 Illustrates fixed number of executed operations 1 operation Only one of them will be executed 2 operations 1 operation 1 operation 1 operation 3 operations
The Big-O Notation Example 1-2 Illustrates dominant operations 2 operations 1 operation 1 operation N times the condition is TRUE + 1 time the condition is FALSE 1 operation N+1 operations 2N operations N operations Executed while the cond. is TRUE N operations 3 operations Only one of them will be executed, take the max: 2 1 operation 2 operations 1 operation 3 operation If the while loop executes N times then: 2+1+1+1+5*N + 1 + 3 + 1 + 2 + 3 = 5N+15
The Big-O Notation How to count for loops for (initialization; condition; increase){ statement1; statement2; . . . } Times the cond. Is TRUE + 1 (when the condition is FALSE 1 op Times the cond. Is TRUE Times the cond. Is TRUE
Example 5 op 1 op 6 op for(i=1; i<=5; i++) for(j=1;j<=5; j++) { cout <<“*”; sum =sum+j; } for(i=1; i<=n; i++) for(j=1; j<=n; j++){ cout <<“*”; Sum=sum + j; } 30 op 25 op 5 op 25 op 2n+2+ 2n2+2n+ 3n2 = 5n2+ 4n+2 25 op 25 op Finally: 147
The Big-O Notation TABLE 1-2 Growth rates of various functions
TABLE 1-3 Time for f(n) instructions on a computer that executes 1 billion instructions per second (1 GHz) Figure 1-4 Growth rate of functions in Table 1-3 The Big-O Notation
TABLE 1-4 Growth rate of n2 and n2 + 4n + 20n The Big-O Notation • As n becomes larger and larger • Term 4n + 20 in f(n) becomes insignificant • Term n2 becomes dominant term
The Big-O Notation • Algorithm analysis • If function complexity can be described by complexity of a quadratic function without the linear term • We say the function is of O(n2)orBig-O of n2 • Let f and g be real-valued functions • Assume f and g nonnegative • For all real numbers n, f(n) >= 0and g(n) >= 0 • f(n) is Big-O of g(n):written f(n) = O(g(n)) • If there exists positive constants c and n0 such that f(n) <= cg(n) for all n >= n0
TABLE 1-5 Some Big-O functions that appear in algorithm analysis The Big-O Notation
Testing and Debugging Good test data Correct output known Include values most likely to causes errors Boundary values Fully exercise code Every line of code is executed by at least 1 test case