伪代码-code complete第9章

之前以为伪代码的作用仅仅是让程序的逻辑更加清晰,并且只能给自己看。今天看了code complete有亮点非常深的体会。首先是伪代码的作用不仅仅是给写代码的使用,可以把它直接作为程序的注释。看一个例子,看看如何写出好的伪代码,并且如何使用它。

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

这是一个写的非常不好的伪代码,首先逻辑不清晰,写的程序让人不明白。作为伪代码,包含了c语言的代码细节*hRsrcPtr。还有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

Endif

Return TRUE if a new resource was created; else return FALSE

       可以看到使用这个代码实现的语言可以不仅用c实现,用其它语言的开发者看到这个伪代码也可以很轻松的实现。原因在于它的封装程度高了一层,用自然语言实现,并且逻辑更清晰。

为什么使用伪代码了?可以看到伪代码的实现和修改非常容易,相当于在语言级别上的编程,不必设计语言细节。在语言细节上的修改代价是非常大的,但是在伪代码层次修改容易。

将伪代码如何修改直接作为注释使用了?再看一个例子:

如何使用这些伪代码了?

 

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

   直接将伪代码作为注释,空白处添加语言代码。这样的过程极好的利用了伪代码,也可以让编程的思路更加清晰。其本质是将过程拆分,也是分割的思想。初看code complete这段的确给我震惊的感觉。伪代码的直接利用。

   总结下就是先写出伪代码,作为理清思路发现错误一个帮助,然后再直接将伪代码作为注释在注释处写出语言代码。例子中是c++代码。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值