Kyle Walsh
Kyle Walsh

Reputation: 2774

How often do you use pseudocode in the real world?

Back in college, only the use of pseudo code was evangelized more than OOP in my curriculum. Just like commenting (and other preached 'best practices'), I found that in crunch time psuedocode was often neglected. So my question is...who actually uses it a lot of the time? Or do you only use it when an algorithm is really hard to conceptualize entirely in your head? I'm interested in responses from everyone: wet-behind-the-ears junior developers to grizzled vets who were around back in the punch card days.

As for me personally, I mostly only use it for the difficult stuff.

Upvotes: 9

Views: 6138

Answers (14)

Rafael Almeida
Rafael Almeida

Reputation: 10712

Steve McConnel's Code Complete, in its chapter 9, "The Pseudocode Programming Process" proposes an interesting approach: when writing a function longer than a few lines, use simple pseudocode (in the form of comments) to outline what the function/procedure needs to do before writing the actual code that does it. The pseudocode comments can then become actual comments in the body of the function.

I tend to use this for any function that does more than what can be quickly understood by looking at a screenful (max) of code. It works specially well if you are already used to separate your function body in code "paragraphs" - units of semantically related code separated by a blank line. Then the "pseudocode comments" work like "headers" to these paragraphs.

PS: Some people may argue that "you shouldn't comment what, but why, and only when it's not trivial to understand for a reader who knows the language in question better then you". I generally agree with this, but I do make an exception for the PPP. The criteria for the presence and form of a comment shouldn't be set in stone, but ultimately governed by wise, well-thought application of common sense anyway. If you find yourself refusing to try out a slight bent to a subjective "rule" just for the sake of it, you might need to step back and realize if you're not facing it critically enough.

Upvotes: 2

Steven A. Lowe
Steven A. Lowe

Reputation: 61233

I don't use pseudocode as it is taught in school, and haven't in a very long time.

I do use english descriptions of algorithms when the logic is complex enough to warrant it; they're called "comments". ;-)

when explaining things to others, or working things out on paper, i use diagrams as much as possible - the simpler the better

Upvotes: 2

Alex B
Alex B

Reputation: 24926

I use it when explaining concepts. It helps to trim out the unnecessary bits of language so that examples only have the details pertinent to the question being asked.

I use it a fair amount on StackOverflow.

Upvotes: 2

Ironsides
Ironsides

Reputation: 263

I and the other developers on my team use it all the time. In emails, whiteboard, or just in confersation. Psuedocode is tought to help you think the way you need to, to be able to program. If you really unstand psuedocode you can catch on to almost any programming language because the main difference between them all is syntax.

Upvotes: 3

Fabio Gomes
Fabio Gomes

Reputation: 6022

I never use or used it.

I always try to prototype in a real language when I need to do something complex, usually writting unit tests first to figure out what the code needs to do.

Upvotes: 1

Juliet
Juliet

Reputation: 81516

I've never, not even once, needed to write the pseudocode of a program before writing it.

However, occasionally I've had to write pseudocode after writing code, which usually happens when I'm trying to describe the high-level implementation of a program to get someone up to speed with new code in a short amount of time. And by "high-level implementation", I mean one line of pseudocode describes 50 or so lines of C#, for example:

Core dumps a bunch of XML files to a folder and runs the process.exe
  executable with a few commandline parameters.

The process.exe reads each file
    Each file is read line by line
    Unique words are pulled out of the file stored in a database
    File is deleted when its finished processing

That kind of pseudocode is good enough to describe roughly 1000 lines of code, and good enough to accurately inform a newbie what the program is actually doing.

On many occasions when I don't know how to solve a problem, I actually find myself drawing my modules on a whiteboard in very high level terms to get a clear picture of how their interacting, drawing a prototype of a database schema, drawing a datastructure (especially trees, graphs, arrays, etc) to get a good handle on how to traverse and process it, etc.

Upvotes: 2

thursdaysgeek
thursdaysgeek

Reputation: 7936

If I'm working out something complex, I use it a lot, but I use it as comments. For instance, I'll stub out the procedure, and put in each step I think I need to do. As I then write the code, I'll leave the comments: it says what I was trying to do.

procedure GetTextFromValidIndex (input int indexValue, output string textValue)
// initialize
// check to see if indexValue is within the acceptable range
//    get min, max from db
//    if indexValuenot between min and max
//       then return with an error
// find corresponding text in db based on indexValue
// return textValue
   return "Not Written";
end procedure;

Upvotes: 2

Chris Cudmore
Chris Cudmore

Reputation: 30151

I don't use pseudocode at all. I'm more comfortable with the syntax of C style languages than I am with Pseudocode.

What I do do quite frequently for design purposes is essentially a functional decomposition style of coding.

public void doBigJob( params )
{
    doTask1( params);
    doTask2( params);
    doTask3( params);
}
private void doTask1( params)
{
    doSubTask1_1(params);
    ...
}

Which, in an ideal world, would eventually turn into working code as methods become more and more trivial. However, in real life, there is a heck of a lot of refactoring and rethinking of design.

We find this works well enough, as rarely do we come across an algorithm that is both: Incredibly complex and hard to code and not better solved using UML or other modelling technique.

Upvotes: 1

Robert Gamble
Robert Gamble

Reputation: 109012

I almost always use it nowadays when creating any non-trivial routines. I create the pseudo code as comments, and continue to expand it until I get to the point that I can just write the equivalent code below it. I have found this significantly speeds up development, reduces the "just write code" syndrome that often requires rewrites for things that weren't originally considered as it forces you to think through the entire process before writing actual code, and serves as good base for code documentation after it is written.

Upvotes: 5

Jake Archibald
Jake Archibald

Reputation:

Fairly rarely, although I often document a method before writing the body of it.

However, If I'm helping another developer with how to approach a problem, I'll often write an email with a pseudocode solution.

Upvotes: 1

Brad8118
Brad8118

Reputation: 4702

I generally use it when developing multiple if else statements that are nested which can be confusing.

This way I don't need to go back and document it since its already been done.

Upvotes: 1

JamesSugrue
JamesSugrue

Reputation: 15011

Mostly use it for nutting out really complex code, or when explaining code to either other developers or non developers who understand the system.

I also flow diagrams or uml type diagrams when trying to do above also...

Upvotes: 1

Elie
Elie

Reputation: 13853

I use it all the time. Any time I have to explain a design decision, I'll use it. Talking to non-technical staff, I'll use it. It has application not only for programming, but for explaining how anything is done.

Working with a team on multiple platforms (Java front-end with a COBOL backend, in this case) it's much easier to explain how a bit of code works using pseudocode than it is to show real code.

During design stage, pseudocode is especially useful because it helps you see the solution and whether or not it's feasible. I've seen some designs that looked very elegant, only to try to implement them and realize I couldn't even generate pseudocode. Turned out, the designer had never tried thinking about a theoretical implementation. Had he tried to write up some pseudocode representing his solution, I never would have had to waste 2 weeks trying to figure out why I couldn't get it to work.

Upvotes: 15

Mark A. Nicolosi
Mark A. Nicolosi

Reputation: 85561

I use pseudocode when away from a computer and only have paper and pen. It doesn't make much sense to worry about syntax for code that won't compile (can't compile paper).

Upvotes: 5

Related Questions