Pseudocode-code complete Chapter 9th

Source: Internet
Author: User

Previously, I thought that the role of pseudo code is only to make the logic of the program clearer, and I can only show it to myself. Today I saw the code complete with a very deep experience. The first is that pseudocode is not only used for writing code, but can be directly used as a program comment. Let's look at an example to see how to write a good pseudocode and use it.

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

This is a very bad pseudo-code. First, the logic is unclear, and the written program is hard to understand. As a pseudo-code, it contains the code details of the C language * hrsrcptr. And return 0, which can be fully encapsulated. The modified code is as follows:

Keep track of current number of resources in use

If another resource is available

Allocate a dialog box structure

If a dialog box structure cocould 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

We can see that the language implemented using this code can be implemented not only in C, but also in other languages, developers can easily see this pseudo code. The reason is that it is encapsulated with a higher level, implemented in natural language, and the logic is clearer.

Why is pseudocode used? It can be seen that the implementation and modification of pseudo code is very easy, which is equivalent to programming at the language level without the need to design language details. The modification cost in terms of language details is very large, but it is easy to modify at the pseudo-code level.

How can I modify the pseudo code directly as a comment? Let's look at another example:

How to use these pseudocodes?

 

  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
  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. }
  39. // If the error code isn' t valid, policy the user that
  40. // Internal error has been detected
  41. Else {
  42. Displayinteractivemessage (
  43. "Internal error: Invalid error code in reporterrormessage ()"
  44. );
  45. }
  46. // Return status information
  47. Return errormessagestatus;
  48. }

Directly comments the pseudo code and add the language code in the blank space. This process makes good use of pseudocode and can also make the programming idea clearer. In essence, splitting the process is also the idea of splitting. First glance

I was shocked by the code complete section. The direct use of pseudocode.

To sum up, write pseudocode first, as a help to clarify the ideas and find errors, and then directly write the pseudo code as a comment in the comments. In this example, C ++ code is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.