虛擬碼-code complete第9章

來源:互聯網
上載者:User

之前以為虛擬碼的作用僅僅是讓程式的邏輯更加清晰,並且只能給自己看。今天看了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++代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.