views:

409

answers:

7

The following code is an example of what I think would qualify as pseudocode, since it does not execute in any language but the logic is correct.

string checkRubric(gpa, major)
    bool brake = false
    num lastRange
    num rangeCounter
    string assignment = "unassigned"
    array bus['business']= array('person a'=>array(0, 2.9), 'person b'=>array(3, 4))
    array cis['computer science']= array('person c'=>array(0, 2.9), 'person d'=>array(3, 4))
    array lib['english']= array('person e'=>array(0, 4))
    array rubric = array(bus, cis, lib)

foreach (rubric as fieldAr)
    foreach (fieldAr as field => advisorAr)
        if (major == field)
            foreach (advisorAr as advisor => gpaRangeAr)
                    rangeCounter = 0
                foreach (gpaRangeAr as gpaValue)
                    if (rangeCounter < 1)
                        lastRange = gpaValue
                    else if (gpa >= lastRange && gpa <= gpaValue)
                        assignment = advisor
                        brake = true
                        break
                    endif
                    rangeCounter++
                endforeach
                if (brake == true)
                    break
                endif
            endforeach
            if (brake == true)
                break
            endif
        endif
    endforeach
    if (brake == true)
        break
    endif
endforeach
return assignment

For the past couple of weeks I've been trying to create a clear definition of what pseudocode actually is. Is it relative to the programmer or is there an actual clearcut syntax? I say pseudocode is any code that does not execute, how about you? Thanks (links to this subject welcome)

+3  A: 

Shamelessly ripped from Wikipedia:

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and subroutines.

There is a lot of code that does not execute. That does not mean it is pseudocode. Your "psuedocode" has a lot of extra stuff that non-programmers will not understand. Instead of being pseudocode, your "psuedocode" language is very, very close to an actual language.

Kevin Crowell
This seems to be the clearest definition.
Cian E
A: 

My two cents on this:

I say pseudocode is any code that does not execute, how about you? Thanks (links to this subject welcome)

That's not what I think of when thinking about its definition. A pseudocode are the steps your program will take to accomplish a task in more detail than describing the algorithm would.

One thing in particular that I find extremely important about how to write a pseudocode is that, it has to be understood by everyone in order for each individual to "port" it to one's desired language. In other words, it does have to be language agnostic.

Just as a constructive criticism, I would not consider your example as pseudocode for various reasons but, specially because, you are using syntax and conventions that resembles a particular programming language. I say pseudocodes should be programming-language agnostic in order to be port to several actual programming languages by different people.

EDIT: Probably one more rule I would add to my definition is that, it has to resemble human language than a programming language. As in, equals instead of ==, assign instead of =. The reason behind this is that, for instance, assignment and equality operators are different in different languages.

Anzurio
@Anzurio, I would argue that "==" is widely used enough that it is acceptable in pseudo-code. However, his use of "as" as well as his use of actual expressions for his conditions instead of English explanations of what is being tested is what makes it not pseudo-code.
Michael Aaron Safyan
@Michael, you are right but that was the first "exemplification" that came to my mind when trying to expose that I'd rather use words or language-agnostic symbology. 'Sides, working with both, VB.NET and C#, that pop up in my mind pretty easily.
Anzurio
+5  A: 

There is no fixed definition of pseudocode. It's any notation that you expect your audience to understand to get your point across. The important idea is that it is intended for humans to read, not computers, so it doesn't have to be precise. You can include the details that are important to your exposition, and omit the ones that are not.

Ned Batchelder
I agree with you 100%. Pseudocode, in my opinion, depends on the context/audience it is provided and doesn't necessarily have to be entirely language independent. This is why I can't shake the belief that pseudocode can approach the limit of just being short of compilable. I think more can be discussed on this question.
Cian E
+1  A: 

Pseudocode should, in theory, be implementation independant. It presents logical steps in plain language of what to do. It is intended for human interpretation, not machine execution.

OP's example is a bit closer to actual code than pseudocode. For example, ++ is not found in all languages. It could also have a very different meaning in others.

MPelletier
+1  A: 

Well, if I don't compile/link my C++ code, it won't execute, so I don't think "Code that doesn't execute" is an acceptable definition.

Likewise scripting languages aren't executed, they're often times interpreted.

My definition of pseudo code would be:

"[Concise] Code that is syntax agnostic, written to convey a function, behavior, or algorithm.""

Alan
Interesting. It sounds like that would just be an essay to me--nothing wrong with writing a proof I guess.
Cian E
Added "concise" for you :)
Alan
+1  A: 

Pseudo-code is any compact, human readable explanation of an algorithm or program. Since your program is not readable to me, I would say that it is not quite pseudo-code. Here is an example of pseudo-code:

def sum(x):
    result = 0
    for each entry in x:
        add current entry to result
    report result

Or, in a slightly different style:

sum(x):
   Let x be an array
   Let result be an integer representing the result, initially 0

   for item in x:
       result += item

   return result

You can use elements of a particular syntax (and, in fact, my pseudo-code tends to look a lot like Python), but it needs to be understandable by a wide audience and should not be obstructed by syntax. For example, I use "+=", but this is because it is highly compact and convenient, not because it is required. If you found "endforeach" helpful and convenient in your exposition, it would have been ok; however, I would argue that such a thing does not belong in pseudo-code as it looks more stinted than helpful or explanatory.

Michael Aaron Safyan
That's the thing, I don't find it convenient at all to write "endforeach," but knew some do that. I included a mixed example, "foreach" is not in every language but "++" almost always is. I used "end" instead of curly brackets. I hardly ever write pseudocode that others would use except myself (usually has a shelf life of 24 hours before it's ported to code), which is why I don't stress about it being language independent. My internal documentation is always in natural language.
Cian E
A: 

Pseudocode is what you'd write on the whiteboard if you want to get your ideas across quickly and clearly. In practice, for me, it's much like an untyped scripting language, but with much looser syntactical requirements. For me it looks much like C because, frankly, most programmers grok some language that is a variant on C syntax and so intuition is easier for more people (it used to look like Pascal, but that's because that was one of the first languages I learned in school).

tvanfosson
True. All my pseudocode never leaves the whiteboard. This is why I think pseudocode, although more typically thought of as closer to natural language, can be stretched into the specific language you and your collaborators will be using. Usually people working on the same project will all use the same language, but then again I only work in small groups or alone.
Cian E