How to go about implementing a problem solution as a program

Course Navigation

A common misconception newcomers have to programming is that when a programmer approaches a problem, he or she just sits down at a computer and starts typing out straight code. (Well, ok, some programmers do this, but it's a bad idea for all but very simple cases as it leads to messy, difficult to maintain code).

Here's an approach to programming problems that has served me well over the years. Let's take an example... something not too complicated... hmm... Ok, let's say you want to make the computer play the Hangman word game. Here's a good way to go from idea to finished program:

1. State the problem and figure out in more detail what you need to accomplish.

You want the computer to play hangman. In more detail, what is that going to require? Well let's think... - We'll need some way for the user to guess a letter (by hitting a key on the keyboard). - We should draw the hanging man somehow... we can represent him by simple characters like so: o       -|- /| - We should display the correct guesses as well as blanks, something like this: H_MB_RG_R

Ok, that gives us a rough idea of what we need to accomplish.

2. Go from your requirements to an English language procedure. (of course you can replace English with your native language :)

Alright, we have some idea of what we need to do, now how can we do it? Well let's go through our requirements one by one, and start programming in English.

Make up a mystery word. We need to wait until the user types a letter on the keyboard. Then we'll check if the letter they guessed is one of the letters in the mystery word. Then we update the mystery word with the guessed letters, if necessary. And then we redraw the man, if necessary. And then we loop back to the beginning and start over. We'll stop looping if the mystery word is fully revealed, or the man gets fully hanged.

Alright, we're halfway there! Note that we haven't needed to deal with a single word of programming language. In fact we could implement this program in just about any language. The algorithm is the important part, the actual implementation is just details; it's been said that 90% of a programmers time is not spent actually writing code, but figuring out how it will work.

3. Go from English to pseudo-code. Pseudo-code isn't really a programming language, but it looks kind of like one. Comments (the text after "//") are an important part of code. Sometimes computer code can be hard to read, so comments help us keep straight what certain things are for, at a glance; it makes the code easier to understand both for other programmers and for ourselves should we come back to this code at some point down the road (think about whether or not you remember the whole script from a movie you saw a long time ago, or just the message behind the movie).

Note that "//" before a line indicates a comment.

//The game has started. We need a word to guess. theMysteryWord = makeUpAMysteryWordRepresentedBy

//While we haven't lost the game, do these things. while theGame ≠ lost  AND  theGame ≠ won

//We need to wait until the user types a letter on the keyboard. waitForALetterFromTheKeyboard; then

//Then we'll check if the letter they guessed is one of the letters in the mystery word. seeIfTheMysteryWordHas( theLetterFromTheKeyboard ); then

//Then we update what's been guessed of the mystery word. update(theMysteryWordGuess); then

//And then we update the drawing of the man. updateTheMan; then

//Do this if and only if we've guessed the mystery word. if theMysteryWordIsFullyGuessed Tell user, "You win!", then set theGame = won; endif //Do this if and only if the man has been hung. if theManIsFullyHanged display("You lose :("); then       display(theMysteryWord); then        set theGame = lost;    endif

//If we made it this far and the game hasn't been lost or won, Go back and see if that'll change the next time through; otherwise, we can go past. endWhile

//Exit the game. exit

4. Alrighty! Now we're 70% of the way there. Time to take our pseudo-code and implement it in our favorite programming language.


 * Next Lesson
 * Previous Lesson
 * Course Home Page