Pseudocode Programming: Think First, Code Second
Pseudocode is simply a way of describing how code should work using plain English.
My favorite programming book, Code Complete Second Edition by Steve McConnell, takes this concept to the next level by dedicating an entire chapter to what he calls the Pseudocode Programming Practice (PPP).
Here's how the PPP works in a nutshell:
- Write the algorithm using pseudocode within code comments
- Write the actual code that implements the pseudocode
- Leave the original comments in place to help inform future users what you were intending (especially useful for code reviews)
Here's an example of the concept from McConnell's book (page 224):
Example of Pseudocode for a Routine
This routine outputs an error message based on an error code
supplied by the calling routine. The way it outputs the message
depends on the current processing state, which it retrieves
on its own. It returns a value indicating success or failure.
set the default status to "fail"
look up the message based on the error code
if the error code is valid
if doing interactive processing, display the error message
interactively and declare success
if doing command line processing, log the error message to the
command line and declare success
if the error code isn't valid, notify the user that an internal error
has been detected
return status information
With the above pseudocode in place, the next steps would be to format the pseudocode as comments and then write the actual code implementation directly below each comment.
Additional Tips
Here are a few additional tips from McConnell regarding PPP:
Try a few ideas in pseudocode, and keep the best (iterate) Try as many ideas as you can in pseudocode before you start coding. Once you start coding, you get emotionally involved with your code and it becomes harder to throw away a bad design and start over.
Constructing classes and constructing routines tends to be an iterative process. Insights gained while constructing specific routines tend to ripple back through the class's design.
Writing good pseudocode calls for using understandable English, avoiding features specific to a single programming language, and writing at the level of intent (describing what the design does rather than how it will do it).
Integrating AI Into the PPP
The Pseudocode Programming Practice is a powerful technique all on its own, but combining it with an AI copilot like Codeium or GitHub Copilot supercharges your productivity, especially for unfamiliar languages. More about that in a future article...