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?
- /* 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 ()){
- // 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;
- }
- // If doing command line processing, log the error message to
- // 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
- }
- Else {
- // Can't do anything because the routine is already Error Processing
- }
- }
- // If the error code isn' t valid, policy the user that
- // Internal error has been detected
- Else {
- Displayinteractivemessage (
- "Internal error: Invalid error code in reporterrormessage ()"
- );
- }
- // Return status information
- Return errormessagestatus;
- }
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.