Warning: stripcslashes() expects parameter 1 to be string, array given in /home/krzyk/coderookie.com/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php on line 89
The Pseudocode Programming Process | Programming tips for newbies - CodeRookie
Warning: stripcslashes() expects parameter 1 to be string, array given in /home/krzyk/coderookie.com/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php on line 142

50% off from Dreamhost hosting plan (that's 59.50$ less for full hosting) - use promo code HOSTING_PROMO during account cration on Dreamhost

Nowalijka - Ĺšledzenie nowych aukcji allegro


Code on a monitor Pseudocode interesting technique that I found in Code Complete, Second Edition by Steve McConnell.

PPP is a way of developing routines by writing pseudocode for them in the first place (not a new topic) and after that, making the pseudocode a comment in the routine (functions, procedure, method or whatever your language calls them). After comments are in the empty routine, the last thing to do is write the code below appropriate comment lines.

I know it isn't the clearest explanation, here look below at the example that I prepared.

We'll work on a routine responsible for query searching in a search engine (based on my very simple & ugly search engine.)

Magellan Maestro 3100 a best value GPS for vehicles, review

First, let's write the header comment for the routine:

  1. // Searches for query in a set of pages, every word
  2. // is searched independently, and then results of all
  3. // search operations are concatenated.

Pretty simple, but should state what the routine will do. Next I will use a simple English statements as pseudocode and write the body of the routine:

  1. // Searches for query in a set of pages, every word
  2. // is searched independently, and then results of all
  3. // search operations are concatenated.
  4.  
  5. // split query into separate words
  6. // for each word in the query
  7.     // get pages containing the word
  8.     // from the database
  9.  
  10.     // for each page
  11.         // if this page is in the score table
  12.             // add current word occurrences on the
  13.             // page to the score table for given page
  14.         // if this page is not in the score table
  15.             // add the page to the score table
  16.             // and store current word occurrences for
  17.             // this page in the score table
  18.  
  19. // create container for pages to return
  20. // for each page in the score table
  21.     // retrieve page info from database and store it
  22.     // in the container
  23. // return the container

It is not the most complicated routine in the word but will do.

Here's the code added for this routine:

  1. // Searches for query in a set of pages, every word
  2. // is searched independently, and then results of all
  3. // search operations are concatenated.
  4.  
  5. function search($query, $from, $amount)
  6. {
  7.   // split query into separate words
  8.   $text = preg_replace("/[^a-zA-Z ]/", "", $query);
  9.   $words = preg_split("/ +/", $text);
  10.  
  11.   // for each word in the query
  12.   foreach ($words as $word)
  13.   {
  14.      // get pages containing the word from
  15.      // the database
  16.      $pages = $this->dataStore->getPagesWithWord($word);
  17.  
  18.      // for each page
  19.      foreach ($pages as $id => $page)
  20.      {
  21.        // if this page is in the score table
  22.        if (isset($this->pages[$id]))
  23.        {
  24.          // add current word occurances on the page
  25.          // to the score table for given page
  26.          $this->pages[$id]["score"] +=
  27.             $page["occurances"];
  28.        }
  29.        // if this page is not in the score table
  30.        else
  31.        {
  32.          // add the page to the score table
  33.          // and store current word occurrences for
  34.          // this page in the score table
  35.          $this->pages[$id] = $page;
  36.        }
  37.      }
  38.   }
  39.   // create container for pages to return
  40.   $webPages = array();
  41.   // for each page in the score table
  42.   foreach (array_keys($this->pages) as $pageId)
  43.   {
  44.     // retrieve page info from database and store in
  45.     // the container
  46.     $webPages[] = $this->dataStore->getPage($pageId);
  47.   }
  48.   // return the container
  49.   return $webPages;
  50. }

The code above is in PHP but the same could be done for any other language, that's the beauty of pseudocode :) . You could give the pseudocode to some other programmer for a review and he doesn't have to know the lang in which it is written.

Some useful notes (once again taken from Code Complete, Second Edition) on how to write the pseudocode:

  1. use English-like words to describe the code
  2. do not use syntactic elements of the programming language
  3. the pseudocode should be written at the level of intent ("whats" not "hows")
  4. it should be enough low level to enable developer to write code based on it

That's it for this post, give me a hint if you liked the code, or better, try to write some more examples of the pseudocode.


  

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • del.icio.us
  • Digg
  • Technorati

8 Responses to “The Pseudocode Programming Process”  

  1. 1 raghu prashanth

    thanks for the information………i think it would help our team in producing a good project.

  2. 2 sairam meghi

    Informative, not at all confusing.
    Try it.

  3. 3 Simi

    Very useful!!! Thanks a lot. It’s going to be of great help in for assignment

  4. 4 nathaniel

    different from other examples… its detailed and easy to understand..

  5. 5 Joshua Tilson

    The whole pseudo Code is now commonly taught in college setting, programming basics and software engineering courses. It really allows you to visualize what the program will do and in what order. The hard part is learning the language well neough to make it do what you want.

  1. 1 PHPGeek » Start with Pseudocode
  2. 2 Coding Guidelines - Commenting « Charismatic Megafauna
  3. 3 Python versus Java &middot Programming tips for newbies - CodeRookie

Leave a Reply