Page 1 of 1

How To Write a Program

Posted: Wed Mar 17, 2010 9:50 pm
by Neo
  1. Analysis: Understand the Problem
    • solve the problem using real data (i.e. generate and solve a simple "test case")
    • explain to someone how to solve the problem
  2. Specification: Describe “what” the task is
    • this is sometimes combined with the Analysis step above
  3. Design: Describe “how” to solve the problem
    • Create an IPO Table with three columns: Input, Processes (calculations), Output
    • "Pseudo-Code the Solution": Pseudo-coding allows you concentrate on the logic of the program before dealing with the details of language syntax (step 3).
    • execute the pseudo-code by hand using a simple “test case” (see Analysis)
  4. Implement the Design
    • Translate Pseudo-Code into Python (or C++)
    • Edit, Compile/Run, and Debug your Python (or C++) Code to obtain a "clean compile"
  5. Test the Code:
    • use good data "test cases" - data that should work
    • use test cases that "test the boundaries" - data covers special cases or
    • use bad data "test cases" - data that would cause the program to crash or return a wrong answer - unless the program is robust enough to take special action.
  6. Maintain the Program: continue to develop the program fixing hidden bugs and adding new and better features
Using Trace Statements
One way to debug a program is to have it display intermediate results. This can be accomplished by inserting output statements that display intermediate values of computations. For example, the following code computes the compound interest for an "amount" for "n" time periods at "interestRate" percent. Each iteration of the loop computes the interest then adds the interest into the amount.

Code: Select all

    for i in range(n): 
        interest = amount * interestRate 
        amount = amount + interest 


Suppose you wanted to see the amount of interest for each iteration of the loop. You could add the following print statement to display interest which could be used to check that the proper interest was being computed.

Code: Select all

    for i in range(n): 
        interest = amount * interestRate 
        amount = amount + interest

        print i, interest 


When done, you could go back into the source code and remove the "trace" statement. However, if you inserted a number of these trace statements in the code, it is more work to go back and physically remove the trace statements. Nevertheless, there is a better way, a trick that allows you to insert trace statements like the above then "turn them off" by changing the value of one constant in the program.

First assign the variable DEBUG equal to the Boolean value True recalling Python is case-sensitive

Code: Select all

    DEBUG = True; 
Then make each trace statement subordinate to an if statement controlled by the boolean variable DEBUG

Code: Select all

     for i in range(n): 
        interest = amount * interestRate 
        amount = amount + interest

        if (DEBUG): 
            print i, interest    
To turn off the trace statements reset DEBUG to False

Code: Select all

    DEBUG = False;