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
● An important topic: preconditionsAn important topic: preconditions and and postconditions
postconditions..
● They are a method of specifying what a function They are a method of specifying what a function accomplishes.
accomplishes.
Preconditions and Postconditions
Preconditions and Postconditions
Frequently a programmer must communicate Frequently a programmer must communicate
precisely
precisely whatwhat a function accomplishes, without any a function accomplishes, without any indication of
indication of howhow the function does its work. the function does its work.
Example
● You are the head of a You are the head of a programming team and programming team and
you want one of your you want one of your
programmers to write a programmers to write a
function for part of a function for part of a
project.
project.
HERE ARE
THE REQUIREMENTS FOR A FUNCTION THAT I
WANT YOU TO WRITE.
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 One way to specify such requirements is with a pair of statements about the function.
pair of statements about the function.
● The preconditionThe precondition statement indicates what must be statement indicates what must be true before the function is called.
true before the function is called.
● The postconditionThe postcondition statement indicates what will be statement indicates what will be true when the function finishes its work.
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
void write_sqrt( double x) // Precondition: x >= 0.
// Postcondition: The square root of x has // been written to the standard output.
...
}
● The precondition and postcondition appear as comments in your
program.
Example
void write_sqrt( double x) // Precondition: x >= 0.
// Postcondition: The square root of x has // been written to the standard output.
...
}
● In this example, the precondition In this example, the precondition requires that
requires that
x >= 0x >= 0
be true whenever the function is be true whenever the function is called.
called.
Example
write_sqrt( -10 );
write_sqrt( 0 );
write_sqrt( 5.6 );
Which of these function calls Which of these function calls
meet the precondition ?
meet the precondition ?
Example
Which of these function calls Which of these function calls
meet the precondition ? meet the precondition ?
write_sqrt( -10 );
write_sqrt( 0 );
write_sqrt( 5.6 );
The second and third calls are fine, since the argument is greater than or equal to zero.
Example
Which of these function calls Which of these function calls
meet the precondition ? meet the precondition ?
write_sqrt( -10 );
write_sqrt( 0 );
write_sqrt( 5.6 );
But the first call violates the
precondition, since the argument is less than zero.
Example
void write_sqrt( double x) // Precondition: x >= 0.
// Postcondition: The square root of x has // been written to the standard output.
...
}
● The postcondition always indicates The postcondition always indicates what work the function has
what work the function has
accomplished. In this case, when the accomplished. In this case, when the
function returns the square root of function returns the square root of xx
has been written.
has been written.
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
is_vowel( 'A' );
is_vowel(' Z' );
is_vowel( '?' );
What values will be returned What values will be returned
by these function calls ?
by these function calls ?
Another Example
is_vowel( 'A' );
is_vowel(' Z' );
is_vowel( '?' );
What values will be returned What values will be returned
by these function calls ?
by these function calls ?
truetruefalse false
Nobody knows, because the Nobody knows, because the
precondition has been violated.
precondition has been violated.
Another Example
is_vowel( '?' );
What values will be returned What values will be returned
by these function calls ? by these function calls ?
Violating the precondition Violating the precondition
might even crash the computer.
might even crash the computer.
Always make sure the precondition is valid . . .
● The programmer who calls the function is The programmer who calls the function is responsible for
responsible for ensuring that the precondition is ensuring that the precondition is valid
valid when the function is called.when the function is called.
AT THIS POINT, MY PROGRAM CALLS YOUR
FUNCTION, AND I MAKE SURE THAT THE PRECONDITION IS
VALID.
. . . so the postcondition becomes true at the function’s end.
● The programmer who The programmer who
writes the function counts writes the function counts on the precondition being on the precondition being
valid, and
valid, and ensures that ensures that the postcondition
the postcondition becomes true
becomes true at the at the function’s end.
function’s end.
THEN MY FUNCTION WILL EXECUTE, AND WHEN
IT IS DONE, THE
POSTCONDITION WILL BE TRUE.
I GUARANTEE IT.
A Quiz
Suppose that you call Suppose that you call
a function, and you a function, and you
neglect to make sure neglect to make sure that the precondition that the precondition
is valid.
is valid.
Who is responsible if Who is responsible if
this inadvertently this inadvertently
causes a 40-day flood causes a 40-day flood
or other disaster?
or other disaster?
YouYou
The programmer who The programmer who wrote that torrential wrote that torrential
function function
NoahNoah
A Quiz
Suppose that you call Suppose that you call
a function, and you a function, and you
neglect to make sure neglect to make sure that the precondition that the precondition
is valid.
is valid.
Who is responsible if Who is responsible if
this inadvertently this inadvertently
causes a 40-day flood causes a 40-day flood
or other disaster?
or other disaster?
YouYou
The programmer who The programmer who calls a function is
calls a function is
responsible for ensuring responsible for ensuring
that the precondition is that the precondition is
valid.
valid.
On the other hand, careful
programmers also follow these rules:
● When you write a function, you should make When you write a function, you should make every effort to detect when a precondition has every effort to detect when a precondition has
been violated.
been violated.
● If you detect that a precondition has been If you detect that a precondition has been
violated, then take appropriate action (repair or violated, then take appropriate action (repair or
terminate).
terminate).
On the other hand, careful
programmers also follow these rules:
● When you write a function, you should make When you write a function, you should make every effort to detect when a precondition has every effort to detect when a precondition has
been violated.
been violated.
● If you detect that a precondition has been If you detect that a precondition has been
violated, then take appropriate action and halt violated, then take appropriate action and halt
the program...
the program...
● ...rather than causing...rather than causing a disaster.a disaster.
Example
void write_sqrt( double x) // Precondition: x >= 0.
// Postcondition: The square root of x has // been written to the standard output.
{
assert(x >= 0);
...
● The assert function is useful for The assert function is useful for detecting violations of adetecting violations of a precondition.
precondition.
Advantages of Using Preconditions and Postconditions
● Succinctly describes the behavior of a Succinctly describes the behavior of a function...
function...
● ... without cluttering up your thinking with ... without cluttering up your thinking with details of how the function works.
details of how the function works.
● At a later point, you may reimplement the At a later point, you may reimplement the function in a new way ...
function in a new way ...
● ... but programs (which only depend on the ... but programs (which only depend on the
precondition/postcondition) will still work with precondition/postcondition) will still work with
no changes.
no changes.
Precondition Precondition
● The programmer who calls a The programmer who calls a function ensures that the
function ensures that the precondition is valid.
precondition is valid.
● The programmer who writes a The programmer who writes a function can bank on the
function can bank on the
precondition being true when precondition being true when the function begins execution.
the function begins execution.
Postcondition Postcondition
● The programmer The programmer who writes a
who writes a
function ensures function ensures
that the that the
postcondition is postcondition is
true when the true when the
function finishes function finishes
executing.
executing.
Summary
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 identfier
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
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