Chapter 9: Building Bigger Programs
How to Design Larger Programs Building something larger requires good software engineering. Design Top- down: Start from requirements, then identify the pieces to write, then write the pieces. Bottom- up: Start building with the pieces you know, test them, combine them, and keep going until you have your program Coding and Debugging: figure out what doesn t work, why, and how to fix it. Done at the same time as you write the code. Testing: make sure the program does what it is supposed to do and does it well. Maintenance: add new features, solved newly discovered bugs goes on for as long as the program is in use.
Top- Down Design Start from a problem statement. What are you trying to do? Refine the problem statement. Use hierarchical decomposition to define subparts. Continue refining until you know how to write the functions. Use procedural abstraction so that higher- level functions are written in terms of lower- level. Procedural abstraction allows us to focus in the big picture instead of on the little details Write functions so they are reusable (e.g. use parameters)
Top- Down Design Top- level function 1 st level Sub- function 1 st level Sub- function 2 nd level Sub- function 2 nd level Sub- function 2 nd level Sub- function 2 nd level Sub- function 3rd level Sub- function 3rd level Sub- function
Example Top- Down Design: An Adventure Game Text- based game The player explores a house moving between rooms To move the player uses commands such as move north, move east, quit, etc.
Example Top- Down Design: An Adventure Game Top- level function: 1. Tell the user how to play the game. 2. Describe the room the user is currently in. 3. Get the player s command. 4. Figure out the next room the user goes to. 5. Return to Step 2, and repeat until the user Quits.
Some new func@ons printnow(): Takes a string as input, and prints it on the Command Area immediately. print() waits until the program is done before it prints. requeststring(prompt): Takes a prompt string as input, accepts a string from the user in a dialog window, then returns the user s input. Other input functions in JES: requestnumber(prompt) requestinteger(prompt) requestintegerinrange(prompt, min, max) Counter part in Python: raw_input(prompt) - for strings input(prompt) for numbers
Some new func@ons JES also provides some output functions: showerror (string) showwarning (string) showinformation (string) These functions display a dialog window with a message
An important new loop A while loop repeats a block of instructions until a test/condition becomes false. Format: while (condition) : instructions false condition true instructions
Beware of infinite loops All loops must eventually terminate Something inside the loop must eventually make the test condition false If a loop does not have a way of stopping, it is called an infinite loop An infinite loop continues to repeat until the program is interrupted Infinite loops occur when: the programmer forgets to write code inside the loop that makes the test condition false. The condition that controls the loop is malformed You should avoid writing infinite loops
Examples stars = 1 while ( stars <= 10) : print * stars = stars + 1 #this next loop is an infinite loop stars = 1 while ( stars <= 10) : print * #this is an infinite loop too stars = 1 while ( stars >= 1) : print * starts = starts + 1
Wri@ng the top level func@on Our top level function, playgame is a direct translation from our outline. Top- level function: 1. Tell the user how to play the game. 2. Describe the room the user is currently in. 3. Get the player s command. 4. Figure out the next room the user goes to. 5. Return to Step 2, and repeat until the user Quits Example: playgame()
Wri@ng the top level func@on This top- level function makes sense, even without knowing the lower level functions (showintroduction, showroom, pickroom) - - lower level function have not been written yet The lower- level functions are being described in terms of their inputs, outputs, and what they should do à Abstraction Once you have written the top- level function, start writing the next level of functions.
Top- Down Design then.. Makes it easier to maintain code Different parts of a program (e.g. different functions) can change without having to change the whole program Allows us to plan functions before we write them Allows us to split the work (the program) among different programmers (or teams) Follow the plan each programmer/team works on a function (or a group of functions)
Top- Down Design playgame() showintro() ShowRoom() pickroom() showkitchen() showporch() showlr() showentryway () showdr()
BoGom- Up Design Start with some idea of what we want to do (problem statement) Instead of refining the problem, focus on building the solution program Reuse code from other programs as much as possible Combine pieces of code from other programs Write new pieces of code/functions as needed Test the program very often
Debugging Debugging: figuring out what the program is doing, how that differs from what we want it to do, and how to fix it. Two kinds of errors: Syntax error: something is wrong with the code itself e.g. misspelled keyword, spacing errors, mismatched parentheses, a forgotten colon The program does not work (it does not run) Python will give you an error message This type of error is easier to fix Logic error: something is wrong with the logic of the program The program works/runs but it does not do what it s supposed to do Python will not give you an error message This kind of error is harder to fix
Tes@ng your program Two main approaches: Glass- box testing: consider how the program is written and test every possible path through the program Black- box testing: consider how the program is supposed to behave and how it responds to valid and invalid inputs Use a combination of these two approaches to test your programs
Tips on Tes@ng and Debugging Try both expected, and unexpected input. Learn to trace code Add print and printnow statements They can help you check the values of variables used in the program or check if you are entering the functions You can delete them later when no longer needed Don t be afraid to change the program Use comments to remove parts temporarily when testing. Use the programming environment debugging tools In JES: showvars() and the Watcher
Seeing the Variables: showvars() This function will show you all the variables and their values at the point when it s executed showvars can also be used in the command area to see the variables created there
Using the Watcher The Watcher allows you to see which lines of code are being executed as they execute Open the Watcher from the main menu in JES then use the command area as normal.
Using the Watcher With the watcher you can: Pause execution of the program Step through the program from a given point Stop execution Speed up / slow down execution Track variables and the values they take
Improving the Adventure Game When testing, we discover: It s hard to tell which room was which when playing the game. Add a printnow in the showroom function to separate or space out one room description form another We can t figure out what we typed where. Add a printnow in the playgame function to display back what the user typed. The user can give an invalid input Add an error message in pickroom and return the user to the room where he/she is currently
Running programs outside of JES Once you make a larger program, you may want to run it in Jython directly. To use the JES libraries in other Jython interpreters, follow these steps in the command line (of the Jython interpreter): 1. >>> import sys 2. Insert the JES sources into your sys.path >>>sys.path.insert(0, r C:\Program Files (x86)\jes 4.3\Sources ) 3. >>> from media import * In a Mac you would follow these steps in the command line (of the Jython interpreter): >>> import sys >>> sys.path.insert(0, /Applications/jes-4-3.app/Contents/Resources/Java/ ) >>> from media import *