开发者

Pseudocode: a clear definition?

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
  开发者_StackOverflow社区          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)


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.


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.


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.


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.


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.""


An outline of a program, written in a form that can easily be converted into real programming statements.

Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. It is simply one step - an important one - in producing the final code. The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜