开发者

Standards for pseudo code? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. 开发者_如何学JAVA

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 3 years ago.

Improve this question

I need to translate some python and java routines into pseudo code for my master thesis but have trouble coming up with a syntax/style that is:

  • consistent
  • easy to understand
  • not too verbose
  • not too close to natural language
  • not too close to some concrete programming language.

How do you write pseudo code? Are there any standard recommendations?


I recommend looking at the "Introduction to Algorithms" book (by Cormen, Leiserson and Rivest). I've always found its pseudo-code description of algorithms very clear and consistent.

An example:

DIJKSTRA(G, w, s)
1  INITIALIZE-SINGLE-SOURCE(G, s)
2  S ← Ø
3  Q ← V[G]
4  while Q ≠ Ø
5      do u ← EXTRACT-MIN(Q)
6         S ← S ∪{u}
7         for each vertex v ∈ Adj[u]
8             do RELAX(u, v, w)


Answering my own question, I just wanted to draw attention to the TeX FAQ entry Typesetting pseudocode in LaTeX. It describes a number of different styles, listing advantages and drawbacks. Incidentally, there happen to exist two stylesheets for writing pseudo code in the manner used in "Introductin to Algorithms" by Cormen, as recommended above: newalg and clrscode. The latter was written by Cormen himself.


I suggest you take a look at the Fortress Programming Language.

This is an actual programming language, and not pseudocode, but it was designed to be as close to executable pseudocode as possible. In particular, for designing the syntax, they read and analyzed hundreds of CS and math papers, courses, books and journals to find common usage patterns for pseudocode and other computational/mathematical notations.

You can leverage all that research by just looking at Fortress source code and abstracting out the things you don't need, since your target audience is human, whereas Fortress's is a compiler.

Here is an actual example of running Fortress code from the NAS (NASA Advanced Supercomputing) Conjugate Gradient Parallel Benchmark. For a fun experience, compare the specification of the benchmark with the implementation in Fortress and notice how there is almost a 1:1 correspondence. Also compare the implementation in a couple of other languages, like C or Fortran, and notice how they have absolutely nothing to do with the specification (and are also often an order of magnitude longer than the spec).

I must stress: this is not pseudocode, this is actual working Fortress code! From https://umbilicus.wordpress.com/2009/10/16/fortress-parallel-by-default/

Standards for pseudo code? [closed]

Note that Fortress is written in ASCII characters; the special characters are rendered with a formatter.


If the code is procedural, normal pseudo-code is probably easy (Wikipedia has some examples).

Object-oriented pseudo-code might be more difficult. Consider:

  • using UML class diagrams to depict the classes/inheritence
  • using UML sequence diagrams to depict the sequence of code


I don't understand your requirement of "not too close to some concrete programming language".

Python is generally considered as a good candidate for writing pseudo-code. Perhaps a slightly simplified version of python would work for you.


Pascal has always been traditionally the most similar to pseudocode, when it comes to mathematical and technical fields. I don't know why, it was just always so.

I have some (oh, I don't know, 10 maybe books on a shelf, which concrete this theory).

Python as suggested, can be nice code, but it can be so unreadable as well, that it's a wonder by itself. Older languages are harder to make unreadable - them being "simpler" (take with caution) than today's ones. They'll maybe be harder to understand what's going on, but easier to read (less syntax/language features is needed for to understand what the program does).


This post is old, but hopefully this will help others.

"Introduction to Algorithms" book (by Cormen, Leiserson and Rivest) is a good book to read about algorithms, but the "pseudo-code" is terrible. Things like Q[1...n] is nonsense when one needs to understand what Q[1...n] is suppose to mean. Which will have to be noted outside of the "pseudo-code." Moreover, books like "Introduction to Algorithms" like to use a mathematical syntax, which is violating one purpose of pseudo-code.

Pseudo-code should do two things. Abstract away from syntax and be easy to read. If actual code is more descriptive than the pseudo-code, and actual code is more descriptive, then it is not pseudo-code.

Say you were writing a simple program.

Screen design:

Welcome to the Consumer Discount Program!
Please enter the customers subtotal: 9999.99
The customer receives a 10 percent discount
The customer receives a 20 percent discount
The customer does not receive a discount
The customer's total is: 9999.99

Variable List:

TOTAL:         double
SUB_TOTAL:     double
DISCOUNT:      double

Pseudo-code:

DISCOUNT_PROGRAM

    Print "Welcome to the Consumer Discount Program!"
    Print "Please enter the customers subtotal:"
    Input SUB_TOTAL

    Select the case for SUB_TOTAL
        SUB_TOTAL > 10000 AND SUB_TOTAL <= 50000
            DISCOUNT = 0.1
            Print "The customer receives a 10 percent discount"
        SUB_TOTAL > 50000
            DISCOUNT = 0.2
            Print "The customer receives a 20 percent discount"
        Otherwise
            DISCOUNT = 0
            Print "The customer does not a receive a discount"

    TOTAL = SUB_TOTAL - (SUB_TOTAL * DISCOUNT)
    Print "The customer's total is:", TOTAL

Notice that this is very easy to read and does not reference any syntax. This supports all three of Bohm and Jacopini's control structures.

Sequence:

Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)

Selection:

if condition
    Do one extra thing

if condition
    do one extra thing
else
    do one extra thing

if condition
    do one extra thing
else if condition
    do one extra thing
else
    do one extra thing

Select the case for SYSTEM_NAME
    condition 1
        statement 1
    condition 2
        statement 2
    condition 3
        statement 3
    otherwise
        statement 4

Repetition:

while condition
    do stuff

for SOME_VALUE TO ANOTHER_VALUE
    do stuff

compare that to this N-Queens "pseudo-code" (https://en.wikipedia.org/wiki/Eight_queens_puzzle):

PlaceQueens(Q[1 .. n],r)

    if r = n + 1
        print Q
    else
        for j ← 1 to n
            legal ← True
            for i ← 1 to r − 1
                if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
                    legal ← False
        if legal
            Q[r] ← j
            PlaceQueens(Q[1 .. n],r + 1) 

If you can't explain it simply, you don't understand it well enough. - Albert Einstein

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜