On writing pseudocode

When we think of algorithms and programs, we usually think of code in some programming language. However, programming languages are languages for humans to talk to machines. For humans to talk to other humans about programs, a programming language is not necessarily the best medium. Enter pseudocode, one of the most misused languages in computer science.

Here are some tips on how to write good pseudocode from the documentation of the algorithms LaTeX package, by Rogério Brito.

Here are some short hints on typesetting algorithms:

  • Don’t overcomment your pseudo-code. If you feel that you need to comment too much, then you are probably doing something wrong: you should probably detail the inner workings of the algorithm in regular text rather than in the pseudo-code.
  • Similarly, don’t regard pseudo-code as a low-level programming language: don’t pollute your algorithms with punctuation marks like semi- colons, which are necessary in C, C++ and Java, but not in pseudocode. Remember: your readers are not compilers.
  • Always document what the algorithm receives as an input and what it returns as a solution. Don’t care to say in the \REQUIRE or in the \ENSURE commands how the algorithm does what it does. Put this in the regular text of your book/paper/lecture notes;
  • If you feel that your pseudo-code is getting too big, just break it into sub- algorithms, perhaps abstracting some tasks. Your readers will probably thank you.

Of course, you should follow those hints with common sense. Well, anything should be done with common sense.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s