21
The Pseudocode Programming Process Chapter 9

The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Embed Size (px)

Citation preview

Page 1: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

The Pseudocode Programming Process

Chapter 9

Page 2: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Summary of Steps in Building Classes and Routines

Page 3: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Steps in Creating a Class

1) Create a general design for the class– Define the class’s specific responsibilities. – Define what “secrets” the class will hide. – Determine whether the class will be derived

from another class, and whether other classes will be allowed to derive from it.

– Identify the class’s key public methods. – Identify and design any non trivial data

members used by the class.

Page 4: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Steps in Creating a Class .. continue

2) Construct each routine within the class - See next slides …

3) Review and test the class as a whole

- After the class as a whole becomes operational, the class as a whole should be reviewed and tested

Page 5: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Steps in Building a Routine

• The major activities involved in creating a routine are – designing the routine, – checking the design, – coding the routine, and – checking the code

Page 6: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Steps in Building a Routine

Page 7: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

• Experts have developed numerous approaches to creating routines, and one of the favorite approach is the

Pseudocode Programming Process

Or PPP

Page 8: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Pseudocode Programming Process

• an informal, English-like notation for describing how an algorithm, a routine, a class, or a program will work.

• defines a specific approach to using pseudocode to streamline the creation of code within routines.

Page 9: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Guidelines for using pseudocode effectively

• Use English-like statements that precisely describe specific operations.

• Avoid syntactic elements from the target programming language.

• Describe the meaning of the approach rather than how the approach will be implemented in the target language.

• Refine the pseudocode in more and more detail until it seems as if it would be easier to simply write the code.

Page 10: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Example of Good PseudocodeKeep track of current number of resources in useIf another resource is available Allocate a dialog box structure

If a dialog box structure could be allocated Note that one more resource is in use Initialize the resource

Store the resource number at the location provided by the caller

Endif EndifReturn TRUE if a new resource was created; else return FALSE

Page 11: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Example of Bad Pseudocode

increment resource number by 1

allocate a dlg struct using malloc

if malloc() returns NULL then return 1

invoke OSrsrc_init to initialize a resource for the operating system

*hRsrcPtr = resource number

return 0

Page 12: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

The previous example is bad Pseudocode because..

• it includes coding details such as *hRsrcPtr in specific C-language pointer notation, and malloc(), a specific C-language function

• It focuses on how the code will be written rather than on the meaning of the design.

• It gets into coding details—whether the routine returns a 1 or a 0

Page 13: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Example

• Write a Pseudocode to read 10 numbers and find out the average

Page 14: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Set default summation to 0

If number of read numbers less than 10

Read another number

Add it to summation

Endif

Divide summation by 10 and print the

result

Page 15: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

The benefits you can expect from using this style of pseudocode

1) Pseudocode makes reviews easier.– You can review detailed designs without examining

source code

2) Pseudocode makes changes easier.– A few lines of pseudocode are easier to

change than a page of code.

3) Pseudocode minimizes commenting effort.– In the PPP, the pseudocode statements

become the comments

4) Pseudocode is easier to maintain than other forms of design documentation.

Page 16: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Constructing Routines Using the PPP

● Design the routine

● Code the routine

● Check the code

● Clean up leftovers

● Repeat as needed

Page 17: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Design the routine• Check the prerequisites• Define the problem the routine will solve• Name the routine• Decide how to test the routine• Think about error handling• Think about efficiency• Research functionality available in the standard

libraries• Research the algorithms and data types• Write the pseudocode• Check the pseudocode• Try a few ideas in pseudocode, and keep the best

(iterate)

Page 18: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Code the Routine

• Write the routine declaration

• Turn the pseudocode into high-level comments

• Fill in the code below each comment

• Check whether code should be further factored

Page 19: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines
Page 20: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Check the Code

• Mentally check the routine for errors

• Compile the routine

• Step through the code in the debugger

• Test the code

• Remove errors from the routine

Page 21: The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines

Clean Up Leftovers

• Check the routine’s interface. (all parameters used)

• Check for general design quality• Check the routine’s data.• Check the routine’s statements and logic.• Check the routine’s layout.• Check the routine’s documentation• Remove redundant comments.