Pseudocode or Code?

what is with the double posting? @jeff!!!

Though, of course, it does not correspond to the pseudo code, which says append A to B, not B to A. That’s a bug, though. :slight_smile:

I often do this, though the pseudocode/comments are only for me to remember what it is that I’m trying to accomplish along the way. It takes no time at all to put down some sentences about what I’m trying to accomplish in English and then flesh out the code afterward.

Sometimes the pseudocode ends up as a comment but often it is deleted as it is clear what the code does.

I use it mostly as a checklist of what I’m hoping to accomplish in the function. The details of how I’m going to do it aren’t essential initially when I’m thinking through the design.

I do this if I’m writing a function complicated enough that I can’t hold every step in my head simultaneously. First just write out each step in a way simple enough to fit into a one-line comment, then fill in the space below each comment with the code for that step.

If you end up with only one line of self-explanatory code for a comment then I can always just delete the comment when I’m done. If the code is somewhat more complicated I leave it in as a useful summary of what that block does.

If a block gets really long/complicated or shows up more than once you extract it out into its own function.

If you debug by reading comments, you are doing it right. I’m tired of finding bugs where what the code DOES and what the comment says it’s SUPPOSED to do don’t match. And, of course, no one can find it, because they have read the comment, so they misread the code into doing what it was supposed to.

That’s why I never read the comments when trying to understand what a code does.

Sure, document what the code should accomplish, and why. But never get into the how, because the only how that matters is

Tilton’s law, learn the damn language.
http://smuglispweeny.blogspot.com/2008/07/now-batting-dave-roberts-why-is-mariano.html

I remember I was once reverse-engineering a file format, and wrote some code to test things (read specific information off the file), apart from a textual spec.

The other guy who was working on the project saw what I had committed to SVN and said

I really hope you understand what all that pseudocode means

oh wait, it’s python

Indeed it was.

I use it sparingly, usually in relation to implementing an image processing or numerical operation.

I’ll also admit that I usually delete my original pseudocode comments and write the algorithm out in the method comment

I often code this way, except I don’t call it pseudo code, I call it English (insert spoken language of choice here). When I go to write a routine and if I have much of the reasoning already worked out, I’ll quickly commit those thoughts as a series of comments. Sure, I’ve been writing code for a long time but I still can’t write code as fast as English.

Sometimes I start writing code first if the the code is quite straight forward but there’s always times where I change my mind about the approach or I realize an additional bit of required logic. In this case, I’ll quickly jot down some more comments so that the new and current lines of thought aren’t lost.

It is true that the resulting comments mirror what is often unambiguous code. The purists generally don’t like this but I firmly believe that code alone only tells you what it does, not what it is supposed to do. So I write what it is supposed to do first.

Cucumber lets me write pseudo code which is used for testing. So I suppose writing pseudo code is part of what I do :slight_smile:

I’m like most others here. I’ll write pseudocode if I have trouble wrapping my brain around it… but then I put the pseudocode in the ide, write the function below it, translating each piece of pseudocode into real code, and then I delete the pseudocode.

Agreed. Pseudo-code is very impractical. All those layers of documentation may make sense for an ISO, but for your average small to mid-sized engineering house, it generates too much overhead.

Personally, I like comments, but only to a point. I generally comment modules (i.e. file headers), functions, and branch statements. Other than that, I agree with Jeff; code should be as human-readable as possible.

As an afterthought, I do write pseudo-code when I’m doing low-level debugging of someone else’s code, so I guess it has a place, albeit limited.

I don’t like pseudocode either. It was extremely useful when I was a beginner, but afterwards it did me no good. It simply wastes time for me nowadays.

And I really don’t like having comments that word the code below them.

Comments that can be made wrong with minor code cleanup suck. Design documents that have to be changed frequently suck. Trying to reason about formal subjects using only informal notation sucks. Writing the same thing in two very similar ways sucks.

(Yeah, I know, you want to know what I really feel.)

Yes, I’m prejudiced. I went for several years with systems analysts thinking that a buggy program written in some sort of pseudocode (actually, they used bastardized decision tables) was an adequate spec for a real program that was actually supposed to work. (Look, guys, I’m the expert in converting specs into programs. Tell me what the program’s really supposed to do and I’ll make it happen.) I was, of course, responsible for any bugs put into the low-level stuff by the analysts, and not given enough context to know they were bugs.

I’ve seen vast arrays of entirely useless comments, which were no more clear, and gave no different information, than the code. Heck, I used to do that sometimes, before I knew better.

I’ve seen comments that described what the code was doing, and been specifically warned away from them, as they were misleading.

Pseudo-code has its uses. It’s good for algorithms books, for example, since it ages well. (I’ve got a book with routines written in Fortran - excuse me, FORTRAN - and APL. Pseudo-code would be nice.) It’s useful as scaffolding in working out a routine, WHEN DONE BY THE GUY WHO’S DOING THE PROGRAMMING.

But don’t use it for design reviews. It’s low-level and untestable, and can be ambiguous. Don’t use it for comments. That’s something I’m just going to have to ignore when maintaining the code, and besides it gives the code the look of commented code, so nobody will put in the actually useful comments. Don’t use it for specifications. It’s on the exact wrong level of abstraction.

I have been known to use the pseudocode approach. It often works great for algorithms, but not much else :(.

Pseudocode has always just been a language-agnostic way of conveying algorithms and data structures to me. I can’t imagine actually writing it out unless I was trying to explain something to somebody who didn’t code or coded in another language.

Personally, I am not really a fan of pseudocode – it is like a programming language with ill-defined syntax and semantics. If in doubt, you have to guess what the author really meant.
Instead, I prefer code written clearly, with real variable and function names (not grmblPsft()-style). The example you’ve given illustrates this quite well: the pseudocode comments do not contain significantly more information than the C version. Of course, you could have written it like this:

st err_stat = fail_st;
Msg err_msg = err_lk( err );
if ( err_msg.vld() ) {
pm err_pm = curr_pm();
if ( err_pm == pm_i ) {
i_msg( err_msg.str() );
err_st = succ_st;
}

In that case, extensive comments and pseudocode would have been useful lest we have to guess what each variable, type, and function means. That’s why I like the rationale behind Ada’s somewhat more complicated (as opposed to C) syntax: code is written once, but read many times; so it should be easy to read, not necessarily easy to write.

That said, I am a sysadmin, not a full-time developer. I write the occasional small utility, and I dig through other people’s code hunting for bugs; but requirements may be different when working on large projects.

Sure, this is stupid:
// lookup the user id
id = lookupUserId();

But that’s a straw man.

What’s meant by PPP is this:
// update the widget on all the ones that matter
for (iterator i=things.begin();i != things.end();++i) {
if (i-isCurrent() i-isImportant() ! skipList.has_key(*i)) {
i-updateWidget();
}
}

When doing the pseudocode, you can skip over the details about EXACTLY what ones that matter means. As long as you are confident that it CAN be determined which ones matter, then you can go on to worry about whether widgets should be updated before or after the foobars are pruned.

Once in a while you discover that you can’t update widgets before OR after foobars are pruned. At that point, you are only 60 seconds into writing the function and you need to look to see if there is a better solution. PPP has improved your efficiency.

Of course, if you never write code that has subtle issues like this, you are either working in a very simple domain, or you are much smarter than me.

I find it easier to think about code in code.

But you shouldn’t be thinking about code. You should think about logic at that point.

Do you write pseudocode before writing code?

Both. At once… Python \o/

+1 :slight_smile: