Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Pseudocode Programming Process

Similar presentations


Presentation on theme: "The Pseudocode Programming Process"— Presentation transcript:

1 The Pseudocode Programming Process
SCMP Special Topic: Software Development Spring 2017 James Skon

2 Steps for Building Classes and Routines
Class construction is an iterative process.

3 Steps in Creating a Class
Create a general design for the class Construct each routine within the class Review and test the class as a whole

4 Steps in Creating a Class
Create a general design for the class Define the class's specific responsibilities Define the classes hidden state (secrets) Define what abstraction the class interface will capture Will the class be derived from another class and with other classes be allowed to derive from it? Identify the class's key public methods What are the data members?

5 Steps in Creating a Class
Create a general design for the class Construct each routine within the class Build the routines! Respond to issues by backtracking Review and test the class as a whole Class should be tested by itself, and then in context.

6 Steps in Building a Routine

7 Pseudocode Informal, English-like notation for describing how an algorithm, a routine, a class, or a program will work. Calculate Pay with Overtime Begin input hours, rate if hours ≤ 40 then pay = hours * rate else pay = 40 * rate + (hours – 40) * rate * 1.5 print pay End

8 Pseudocode Use English-like statements that precisely describe specific operations. Avoid syntactic elements from the target programming language. Write pseudocode at the level of intent.

9 Pseudocode increment resource number by 1
BAD! 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 Keep track of current number of resources in use If 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 Return true if a new resource was created; else return false Good!

10 Why Pseudocode? Pseudocode makes reviews easier.
Pseudocode supports the idea of iterative refinement. Pseudocode makes changes easier. Pseudocode minimizes commenting effort. Pseudocode is easier to maintain than other forms of design documentation.

11 Constructing Routines
Design the routine. Code the routine. Check the code. Clean up loose ends. Repeat as needed.

12 Design the routine Create a specification:
ReportErrorMessage() takes an error code as an input argument and outputs an error message corresponding to the code. It's responsible for handling invalid codes. If the program is operating interactively, ReportErrorMessage() displays the message to the user. If it's operating in command-line mode, ReportErrorMessage() logs the message to a message file. After outputting the message, ReportErrorMessage() returns a status value, indicating whether it succeeded or failed.

13 Design the routine Check the prerequisites
Is the job well defined? Define the problem the routine will solve he information the routine will hide Inputs to the routine Outputs from the routine Preconditions guaranteed to be true before the routine is called Postconditions guarantees will be true after routine Name the routine

14 Design the routine Decide how to test the routine
Research functionality available in the standard libraries Think about error handling Think about efficiency Research the algorithms and data types Write the pseudocode

15 Write the pseudocode Start with the general and work toward something more specific. Write a concise header statement ReportErrorMessage This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure.

16 Write the pseudocode ReportErrorMessage
This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure. set the default status to "fail" look up the message based on the error code if the error code is valid if doing interactive processing, display the error message interactively and declare success if doing command line processing, log the error message to the command line and declare success if the error code isn't valid, notify the user that an internal error has been detected return status information

17 Write the pseudocode Think about the data – What are the data elements, how appropriate are they? Check the pseudocode Try a few ideas in pseudocode, and keep the best (iterate)

18 Code the Routine

19 Code the Routine - declaration
/* This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure. */ <-- 1 Status ReportErrorMessage( ErrorCode errorToReport ) set the default status to "fail" look up the message based on the error code if the error code is valid if doing interactive processing, display the error message interactively and declare success if doing command line processing, log the error message to the command line and declare success if the error code isn't valid, notify the user that an internal error has been detected return status information Header comment interface statement

20 Code the Routine – first and last statement
/* This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure. */ Status ReportErrorMessage( ErrorCode errorToReport ) { // set the default status to "fail" // look up the message based on the error code // if the error code is valid // if doing interactive processing, display the error message // interactively and declare success // if doing command line processing, log the error message to the // command line and declare success // if the error code isn't valid, notify the user that an // internal error has been detected // return status information } pseudocode statements from here down have been turned into C++ comments

21 Code the Routine – Fill in the code
Programming Process /* This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure. */ Status ReportErrorMessage( ErrorCode errorToReport ) { // set the default status to "fail" Status errorMessageStatus = Status_Failure; // look up the message based on the error code Message errorMessage = LookupErrorMessage( errorToReport ); // if the error code is valid if ( errorMessage.ValidCode() ) { <-- 1 // determine the processing method ProcessingMethod errorProcessingMethod = CurrentProcessingMethod(); // if doing interactive processing, display the error message // interactively and declare success if ( errorProcessingMethod == ProcessingMethod_Interactive ) { DisplayInteractiveMessage( errorMessage.Text() ); errorMessageStatus = Status_Success; } The code for each comment has been filled in

22 Code the Routine – Fill in the code
// if doing command line processing, log the error message to the // command line and declare success else if ( errorProcessingMethod == ProcessingMethod_CommandLine ) { CommandLine messageLog; if ( messageLog.Status() == CommandLineStatus_Ok ) { messageLog.AddToMessageQueue( errorMessage.Text() ); messageLog.FlushMessageQueue(); errorMessageStatus = Status_Success; } else { // can't do anything because the routine is already error processing // if the error code isn't valid, notify the user that an // internal error has been detected DisplayInteractiveMessage( "Internal Error: Invalid error code in ReportErrorMessage()" ); // return status information return errorMessageStatus; good candidate for being further decomposed

23 Code the Routine Check whether code should be further factored
In some cases, you'll see an explosion of code below one of the initial lines of pseudocode. Factor the code below the comment into a new routine.

24 Check the Code After designing and implementing the routine, the third big step in constructing it is checking to be sure that what you've constructed is correct. Any errors you miss at this stage won't be found until later testing.

25 Compile the routine Set the compiler's warning level to the pickiest level possible. Use validators. (lint) Eliminate the causes of all error messages and warnings.

26 Test the code Test the code using the test cases you planned or created while you were developing the routine.

27 Clean Up Leftovers Check the routine's interface.
Check for general design quality. Check the routine's variables. Check the routine's statements and logic. Check the routine's layout. Check the routine's documentation. Remove redundant comments.

28 Code the Routine

29 Code the Routine - Example
/* This routine outputs an error message based on an error code supplied by the calling routine. The way it outputs the message depends on the current processing state, which it retrieves on its own. It returns a value indicating success or failure. */ Status ReportErrorMessage( ErrorCode errorToReport ) set the default status to "fail" look up the message based on the error code if the error code is valid if doing interactive processing, display the error message interactively and declare success if doing command line processing, log the error message to the command line and declare success if the error code isn't valid, notify the user that an internal error has been detected return status information Header comment turned into a c++ comment Interface statement

30 Code the Routine - Example
Status ReportErrorMessage( ErrorCode errorToReport ) { // set the default status to "fail" Status errorMessageStatus = Status_Failure; // look up the message based on the error code Message errorMessage = LookupErrorMessage( errorToReport ); // if the error code is valid // if doing interactive processing, display the error message // interactively and declare success // if doing command line processing, log the error message to the // command line and declare success // if the error code isn't not valid, notify the user that an // internal error has been dtected // return status information Code filled in New variable for errorMessage

31 Check the Code After designing and implementing the routine, the third big step in constructing it is checking to be sure that what you've constructed is correct. Mentally check the routine for errors Understand “why” the routine works Compile the routine Let the compiler look for errors Step through the code with a debugger Test the program

32 CLEAN UP LEFTOVERS Check the routine's interface..
Check for general design quality. Check the routine's variables. Check the routine's statements and logic. Check the routine's layout. Check the routine's documentation. Remove redundant comments.


Download ppt "The Pseudocode Programming Process"

Similar presentations


Ads by Google