Jump to content

Pseudocode


ScottR

Recommended Posts

I currently use pseudo code, but am thinking I am not using it properly. Here's an example of what I may write for a script that displays content depending on what is passed through a URL.(very simple for display purposes)1. If url does not contain ? then display page 1 else if url contains id get id from url else display error message and kill script2. If id contains invalid data then display error message and kill script else connect to database, select data, and display back to screenI like the idea of using pseudo code, but am not sure I am using it properly.Do you use pseudo code? If so, is it more specific? If not, what is your method?Thanks, Scott

Link to comment
Share on other sites

What is a planning phase?...just kidding.I usually don't get detailed enough in my planning phase to use psuedo code. I like to use bulleted lists to outline what a particular class/module needs to accomplish. For me anyway, getting too detailed is just throwing time away since you usually encounter situations you didn't anticiapte once you start writing the code.

Link to comment
Share on other sites

Pseudo-code was useful when I was learning how to program, when I knew conceptually what I wanted the program to do but not how to write it. Now that I know the language sometimes it's just as fast to write the actual code as it is the pseudo-code, or I'll also just write psuedo-code using comments. If I'm working on a big piece of code and I get to something like a loop or if statement where I want to keep going on the next thing, I'll sometimes make some notes in comments about what will eventually go there.

		if (count($response['errors']) == 0)		{		  if ($insert['type'] == 1 && $insert['track_type'] == 'scorm') // SCORM content		  {			// process the SCORM package			if (!file_exists($dir . 'imsmanifest.xml'))			{			  $response['errors']['reason'] = 'The SCORM package did not contain a required imsmanifest.xml file.';			}			else			{			  // open and parse the manifest			  // look for the organizations node			  // find the default organization			  // insert default org into database			  // look for the resources node			  // find all SCOs			  // insert SCOs into database			}		  }		}

There's not really a "right" or "wrong" way to use pseudo-code though, anything that you're having trouble programming can probably be made easier if you give yourself an easy to follow list of steps like pseudo-code.

Link to comment
Share on other sites

Thanks for the replies. Good stuff...@aspnetguyI often use a method similar to yours. I just list objectives, what the script is meant to accomplish. That seems to work well for me.@justsomeguyThat's a good idea. Damn, that seems like a no-brainer now that I think about it. Just write the loops, functions, etc.. with comments and go back and fill them in the variables and details.The one thing I like about pseudo code is that it forces me to consider if I am using the best approach before I start writing code. I have had several times when I would write a class and then decide I wanted to do things differently.Thanks, Scott

Link to comment
Share on other sites

Guest FirefoxRocks

I didn't realize it was called pseudocode, but I do this whenever I encounter an error which I can't figure out. I go step by step through the program/function and go through exactly what each line is doing.This is even faster with a debugger, but sometimes you don't know exactly why the error is there even though you know what the error is. That's how it (pseudocode) helps me anyways.

Link to comment
Share on other sites

lol, thanks but was just looking for a blurb about it.
That's ok, I was responding to the original poster. There's a link to wikipedia describing pseudo-code in general if you just want to know what it is.
wut platform does pseudocode run on?and how is it particulary usefull?
It's not executable code, it's an English-language description of the program you haven't written yet. It's useful when you're designing your application so that you can get the whole thing planned out before you start writing the actual code. It's a lot easier to understand a page of pseudo-code describing an algorithm then it is to read through the actual algorithm.
I didn't realize it was called pseudocode, but I do this whenever I encounter an error which I can't figure out. I go step by step through the program/function and go through exactly what each line is doing.
That's not necessarily psuedo-code, that's just walking through the program. In debugger language that's called "stepping through". You can use breakpoints to help with this, if you set a breakpoint in the code then when the computer gets to that line it will stop and let you look at what's going on before it goes to the next line. You can set breakpoints and step through Javascript with Firebug, for example. I've used that several times to help myself figure out what's going on. That's not really pseudo-code though. A piece of psuedo-code would be something like this, for a login page:
if the form is submitted:  get the username  get the password  look up the user in the database and get the password  if there are no matches:	show an error that the user was not found and quit  if the password in the database does not match the form password:	show an error that the password was incorrect and quit  store the user's information in the session  redirect to the user page

That's not going to run anywhere, that's not executable code, but it's easier to read that and get a description about what to do then it is to read the actual code that does that and figure out what it's doing. Some of those sentences will map to a single line of code (e.g. "get the username" == "$user = $_POST['username'];"), and some of them map to multiple lines of code (e.g. "look up the user in the database and get the password").

Link to comment
Share on other sites

I think it mite be usefull, but its kind of pointless, id rather go directly and plan simply on the basic structure. I'm a hands on person, I hate outlining or annotating an essay or a work of literature. Computers are the same with me, its just my views. It looks okay, but its not something for me.

Link to comment
Share on other sites

It depends on the project. If you're making a login page you don't really need a design document. If you're building a large, complex application, you can't do it unless you have a design document that outlines everything the program does and how the different parts work together. If you don't use that type of stuff you'll run into all kinds of problems, the least of which are new requirements from the customer where you can't tell them no because you don't have a document saying exactly what you're going to do. They teach these kinds of techniques in software engineering classes to help manage large projects (although pseudo-code specifically was introduced in CSE101 and used for every class from then on). Some clients even require that you produce things like this as the first step in the application design. The projects that I work on that are months-long projects all have documentation describing exactly what they do and how they do it. Those documents are (paid) deliverables for the project before any code is ever written. The client wants to see exactly what you're going to do before they pay you to start doing it, it doesn't really matter if that's the way you work or not. I can keep a lot of things in my head at once but I would rather commit program designs to paper then storing that in my head.

Link to comment
Share on other sites

Heh. My Software Development (subject) textbook has all its examples written in very cryptic pseudo-code that only the authors could possibly fully understand... but I suppose that's what happens when there is no programming language specified for the course, even if 99% of schools seem to choose VB6.

Link to comment
Share on other sites

Heh. My Software Development (subject) textbook has all its examples written in very cryptic pseudo-code that only the authors could possibly fully understand... but I suppose that's what happens when there is no programming language specified for the course, even if 99% of schools seem to choose VB6 be stuck in 1998.
fixed that for youI fail to see why any reputable school would be teaching a language that 3 versions out of date...they are doing the students a dis service by teaching VB.
Link to comment
Share on other sites

Thankfully I don't think many of the world-class universities are using VB as a teaching tool these days. I think most of them are probably starting people with Java, the last I heard at ASU you could choose either Java or C++ as a starting language. Ada is another fairly popular language for teaching concepts. I would be pretty surprised if there was a reputable school with a computer science chair who thought that VB was the best way to go for new students.

Link to comment
Share on other sites

Heh. My Software Development (subject) textbook has all its examples written in very cryptic pseudo-code that only the authors could possibly fully understand... but I suppose that's what happens when there is no programming language specified for the course, even if 99% of schools seem to choose VB6.
One of the main purposes of pseudo code is that it is supposed to be independent of any programming language. Just an algorithm at a high level. That kinda defeats the purpose.
Link to comment
Share on other sites

Yeah pseudo-code should be easy to understand. You see it a lot in textbooks though, where the author wants to illustrate a certain algorithm like a bubble sort or something but doesn't want to confuse the student with the syntax of any specific language that they may or may not know, so they just write the algorithm out in pseudo-code. In fact, here's the bubble sort algorithm in pseudo-code:

procedure bubbleSort( A : list of sortable items ) defined as:  for each i in 1 to length(A) do:	 for each j in length(A) downto i + 1 do:	   if A[ j -1  ] > A[ j ] then		 swap( A[ j - 1],  A[ j ] )	   end if	 end for  end forend procedure

People seem to use VB-like syntax for pseudo-code. Anyway, you can take that algorithm and go through it line by line to see what it does, and implement it in a language that you know. The first line there just defines the bubblesort function (they call it a procedure; same thing), which takes one argument (the list of items to sort). The function has one main loop, which loops over every element in A (the list). Inside that loop, for each element, it loops backwards through the list. For each element it finds going from back to front, it compares that element with the element right before it. If the element before is greater, then it swaps the two elements and continues looping. This algorithm could be optimized to be more efficient, but that's the general idea of a bubble sort (the larger elements "bubble" up towards the end of the list). So you can take a piece of pseudo-code like that, and translate it to a language like PHP. The first line is pretty easy to translate:procedure bubbleSort( A : list of sortable items ) defined as:

function bubbleSort ($A)

The first two for loops are also straightforward: for each i in 1 to length(A) do: for each j in length(A) downto i + 1 do:

function bubbleSort ($A){  for ($i = 0; $i < count($A); $i++)  {	for ($j = count($A); $j > $i; $j--)	{

Then we have the if statement and the swap. The psuedo-code just uses a function to swap the two elements, but PHP doesn't have a function to do that (or at least I'll assume it doesn't). So the swap line will be replaced by a few lines that will swap the two values.

function bubbleSort ($A){  for ($i = 0; $i < count($A); $i++)  {	for ($j = count($A); $j > $i; $j--)	{	  if ($A[$j - 1] > $A[$j])	  {		// swap $A[$j - 1] and $A[$j]		$temp = $A[$j - 1];		$A[$j - 1] = $A[$j];		$A[$j] = $temp;	  }	}  }  return $A;}

So there's an implementation of the bubble sort algorithm in PHP that I wrote simply by following the pseudo-code. I could make that in any language if I knew the syntax for that language. That's the main purpose of using pseudo-code, so that you describe what a program does without focusing on the specific language used.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...