fengcarl

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

设计子程序的步骤:

1、检查先决条件 --检查这个程序是否真正必须

2、定义子程序要解决的问题

详细列出如下内容:

这一子程序将要隐藏的内容;

传给这个子程序的各项输入;

从该子程序得到的输出;

eg.ReportErrorMessage()

1)隐藏了两个信息,一个是错误信息的文本,另一个是当前的处理方式

3、为子程序命名

4、决定如何测试子程序,单元测试

5、在标准库中搜寻可用的功能

6、考虑错误处理。考虑所有可能出错的环节

7、考虑效率问题

8、研究算法和数据类型

9、编写伪代码

1)一个子程序的头部注释

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.

2)编写伪代码

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

 

编写子程序的步骤:

写出子程序的声明

 1 /*This routine outputs an error message based on an error code supplied by the calling routine.
 2 
 3 The way it outputs the message depends on the current processing state, which it retrieves on its own. 
 4 
 5 It returns a value indicating success or failure.*/
 6 
 7 //声明
 8 
 9 Status ReportErrorMessage(
10 
11       ErrorCode errorToReport
12 
13 ){
14 
15 //set the default status to "fail"
16 Status errorMessageStatus = Status_Failure;
17 
18 //look up the message based on the error code
19 Message errorMessage = LookupErrorMessage(errorToReport);
20 
21 //if the error code is valid
22 if( errorMessage.ValidCode() ){
23 //determine the processing method
24 ProcessingMethod errorProcessingMethod = 
25   CurrentProcessingMethod();
26 
27 //if doing interactive processing ,display the error message 
28 //interactively and declare success
29 if(errorProcessingMethod == ProcessingMethod_Interactive) {
30 DisplayInteractiveMessage( errorMessage.Text() );
31 errorMessageStatus = Status_Success;
32 }
33 
34 //if doing command line processing ,log the error message to the command line and declare success
35 else if(errorProcessingMethod == ProcessingMethod_CommandLine) {
36 CommandLine messageLog;
37 if(messageLog.Status()== CommandLineStatus_OK){
38 messageLog.AddToMessageQueue(errorMessage.Text());
39 messageLog.FlushMessageQueue();
40 errorMessageStatus = Status_Success;
41 }
42 else {
43 }
44 else {
45 }
46 //if the error code isn't valid , notify the user that an internal error has been detected
47 else{
48 DisplayInteractiveMessage("Internal Error: Invalid error code in ReportErrorMessage()");
49 }
50 
51 //return status information
52 return errorMessageStatus;
53 }

 

posted on 2013-09-24 19:09  fengcarl  阅读(321)  评论(0编辑  收藏  举报