31

I've found questions answering where or when to write pseudocode in scientific papers, but I haven't found any addressing any sort of convention for pseudocode, such as syntax, what type of symbols or notation to use and what they mean precisely, or any sort of 'formal' definition of pseudocode. I suppose that's the nature of pseudocode, it should be understandable and general no matter how you write it, but I wish there was some sort of guidance for how to structure and convey meanings across in a formal manner. Does this exist? For now I am purely imitating the CLRS pseudocode style.

1
25

As you are already stating: There is no norm / convention for pseudocode.

Personally, I prefer a python-like notation, but this is because I like python ;-). CLRS is fine for many purposes.

If your software is written in a specific language, it might be beneficial to use a code convention which allows you to express the specifics of your programming language - this reduces the risk of introducing errors.

Even more important then the code convention is, that you should ensure your pseudocode allows someone else to re-implement your code without room for interpretations. Sometimes, "real" code can be more appropriate (but yes, it can be lengthy).

22

A de-facto sort-of-convention is using one of the LaTeX algorithm listing packages, e.g. algorithm2e, algorithms or algorithmicx.

Each of the packages have a documentation PDF which includes several examples you can follow, and have multiple in-built commands for things like conditions, repetition, clauses, etc. Alternatively, you can have a look at the Algorithms section of the LaTeX WikiBook, which is shorter and simpler and mentions all three. Pick one and use it.

Here's a brief example:

\usepackage[]{algorithm2e}
% ... etc. etc. ...

\begin{document}

% ... etc. etc. ...

\begin{algorithm}[H]
 \KwData{this text}
 \KwResult{how to write algorithm with \LaTeX2e }
 initialization\;
 \While{not at end of this document}{
  read current\;
  \eIf{understand}{
   go to next section\;
   current section becomes this one\;
   }{
   go back to the beginning of current section\;
  }
 }
 \caption{How to write algorithms}
\end{algorithm}
% ... etc. etc. ...
\end{document}

and this produces:

rendered algorithm here

Now, even if you don't write your documentd with LaTeX, you could write just your pseudocode that way and use the resulting PDF, or just copy the resulting text into whatever editor you are using.

13

Edsger Dijkstra used a notation in most of his papers that was also adopted by David Gries in The Science of Programming. If you are happy enough with a procedural approach and don't require functional or object-oriented notations then it is a pretty good choice with a long history.

8

You're over-thinking this. Pseudocode is not a formal language with formal syntax. Just write whatever most clearly expresses what you need to express. Something like CLRS is a good starting point.

7
  • 4
    I don't agree. Exactly because it is not a formal language, the question for conventions seems like a necessity. The dangerous thing I see are people underestimating the usefulness of conventions and just "do whatever pleases them". Compare with science (say physics): using F for force, a for acceleration, = for equal etc. is purely conventional. For example и+w\щ (и being the force, + being the equal, щ the mass, \ the multiplication and w the acceleration) is (even with the definition in brackets) clearly worse than writing F=a*m. Only because of sticking to conventions or not. – Mayou36 Aug 25 '18 at 9:55
  • 4
    @Mayou36 Ironically, that’s normally written F=ma. But the point here is that, unlike Newton’s laws, there is no single convention for how pseudocode should be written so I don’t think your argument has a lot of force. – David Richerby Aug 25 '18 at 10:59
  • 1
    I do not understand your point of what exactly is ironically? Do you refer to the order? Or the * sign? This is unimportant... "there is no single convention" THAT'S an answer! You did not include that in yours. Read again the question, he explicitly asked for a convention. Stating that there is none is an answer on that. Stating that it is not a formal language is not an answer. You answered on "What is the exact syntax of pseudo code?" – Mayou36 Aug 26 '18 at 20:47
  • @Mayou36 It’s ironic that you made a big long comment about how conventions are important, and then you quoted Newton’s second law in an unconventional way (F=a*m instead of F=ma; I’m referring to both the order and the explicit multiplication sign). – David Richerby Aug 26 '18 at 21:08
  • There is, to me (physicist here), no known convention on the order of "m" and "a". Why do you think there is? The multiplication sign can be written or not, but if it is written, it is written as *. If there are conventions, stick to them. That's the point. We don't want to waste time here, I made a constructive comment, trying to neat picking does not change the point. – Mayou36 Aug 28 '18 at 17:20
4

Let me add some more thoughts on this. Yes there is no guide and I think a convention could make things only more complicated than easier.

For example it could depend on how deep you want to go into details. Showing an implementation of some algorithm is done more programming-like, closer to programming languages.
If you talk about a particular language in your paper, you can focus on this language more, use language specific elements and so on, than if it's a general text. Hold a general text really independant of languages.

In contrast if you only explain the principle of an algorithm, this can happen textual without any code or code-like part and it will be shorter and more comprehensive than something using code elements. You can write for example

First of all find the largest and smallest number in your list.
Then calculate A, B and C from your numbers.
Next test if numbers X and Y match some criteria.
some more steps what to do ...
Now if something matches, do step Z and you have found your solution.

The textual form gives an overview of what has do be done. The reader can understand what happens and why this happens, instead of deriving the algorithm from lots of for and if. But I would call this text pseudo code too, because it describes all steps in a very abstract but still algorithm-like language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.