Thursday 3 September 2009

On the semantics of pseudo-code

I was reading a thread on HackerNews entitled "What language do you think in?" and I was fascinated by the responses.

Many of those commenting claimed to think "in pseudo-code". That got me thinking - what does it mean to think in pseudo-code?

By the textbook definition, pseudo-code is some sort of idealised, abstract code with an informal syntax. It serves as an aid to human understanding, making use of the abilities of humans to resolve ambiguity in a sensible way and "black box" a lot of minor details. That all sounds fine.

The catch for me comes in thinking about the semantics of this mental pseudo-code. It still has some semantics, I assume, otherwise it is likely to be entirely useless as a mental model. Perhaps we think of a statement in pseudo-code for "sum all of the values in X":
for each value k in X
a = a + x
OK, so that seems a reasonable enough translation from an English language specification into some code. We've left out some details like what the initial value of "a" is, and the syntax is pretty fluid, but it should be unambiguous to most readers.

The problem is that this is only one step of nearly-mechanical translation from just writing code in a programming language. There is a sneaky step of concretisation going on here, both in the framing of the statement, and the pseudo-code representation.

First, in the framing of the statement, we've assumed a stored list of values exists and is called "X". Even abstracting this away to "the list of input values" (referring to it by its purpose, rather than a label) is still commitment to a paradigm at this early stage. On the language side, we've basically just taken the semantics of some reasonably ubiquitous "C-style" language (think C, Python, Ruby, whatever) and abstracted away some syntax. This really doesn't seem very useful in terms of "abstract thinking".

So is this really what people mean when they say "thinking in pseudo-code"? If so, that's just really a coded way of saying "I mentally construct programs in an idealised version of language X", where 'X' is some language (or perhaps group of similar languages) that the programmer is familiar with.

I'm really not a fan of early concretisation. I can't think how many times I've been in a meeting discussing high-level requirements with a group of technical and non-technical people. The conversation goes something like:

Non-Technical Person: We're really going to need X
Me: Are you sure you need X? Y seems to do everything you want equally well, and it might fit better with the established processes of the organisation.
Non-Technical Person: The processes that are important are...
Technical Person 1: ... we should do Z! because there will just be a couple of classes to add, and using language version 2.3.7 we can just use built-in serialisers to create an object store.
Technical Person 2: No, 2.3.7 will require a kernel upgrade, we should use 2.3.6 and avoid that altogether.
(...)
Me: *head explosion*
Names and specifics (and a little dramatic effect) aside, I've really had this exact conversation more than once in more than one organisation. What I've come to realise is that there are a lot of people who refuse to engage in a conversation in the abstract, and they have to mentally compile to code or implementation details in order to even engage with the conversation. I think this is probably the result of people who genuinely do think in pseudo-code in the way that I've described.

Naturally, this is all speculative, because I'm not sure I could force myself to think in code if I tried. I'm just not wired that way. I need to have a high-level understanding of a problem (which allows me to mentally formulate a high-level solution - usually in terms of the same abstract concepts used to describe the problem), and then "programming" becomes a typing exercise, in which I mentally translate from a high-level computational model to the code I'm writing as I type it.

So, I'm interested in hearing from people who do claim to think in 'pseudo-code'. Have I completely misrepresented what you mean when you say it? Are you actually writing in a language with syntax in your head? Do you close your eyes and see functions and objects and whatever else? Similarly, if you don't think like that, have you observed others that do?

4 comments:

  1. I don't really 'think' in pseudocode, I'm definitely a fan of thinking in high level concepts. I find diagrams and scribbles are more effective for organising my thoughts about a problem than pseudocode.

    I will often use pseudocode when jotting down possible implementations of a solution, but it has fairly loose semantics and syntax (whatever makes the most sense) for the purpose of conveying the idea of a process/program to someone else, or me in the future.

    ReplyDelete
  2. I try to keep my code in the domain language as much as possible, especially when writing pseudocode, for example if I was thinking about some seat allocation problem (no idea what, first thing that came to mind) I might say

    'for each seat in auditorium'

    But I'm not thinking about it in any specific language/paradigm, seats in the auditorium is conceptually 'a collection'

    I almost never 'type out' pseudocode, it's mostly when I am scribbling on paper, and it will often be mixed with diagrams where that makes more sense.

    ReplyDelete
  3. I personally love a whiteboard for this kind of stuff, I sorta start by sketching out what I may need (data structures being the important thing) and may name some functions but typically just assume they do what they imply. Its more control-flow mock up kind of stuff with little detail that I will do on the board. Its pretty nice in that its fairly paragdim nuetral too

    and with your example
    for each value k in X
    a = a + x

    dont you mean
    a = a + k

    ReplyDelete
  4. I tend to also think largly in a functional way, it tends to map back fairly well, as long as its not pushed to far

    ReplyDelete