Programming Concepts
Program Development Methodology
or
How to Write a Computer Program from Scratch

1. First make sure you know how to solve the problem by hand. Writing a computer program is essentially giving the computer instructions on how to solve a problem. But you can't give someone else instructions on how to do something until you know how to do it yourself. So start by working out a few problems by hand, to make sure you know what steps are involved in the solution and what landmines to watch out for ( like dividing by zero. ) This step will also provide you with some nice test cases to which you know the answer, for testing your program later.

2. Based on your experience from step 1, write out the basic steps in your algorithm using pseudocode. You don't have to concern yourself with the rules, syntax, or limitations of any particular computer language at this point. You may want to just assume that everything goes well at this point, and worry about problem cases later, or you may want to include the consideration of problems ( like dividing by zero ) in your pseudocode.

2. Start your actual code with the boilerplate framework that you know goes into every program. For example, every C++ program you write ( as a new programmer ) will have the following general structure:

		/* ProgramFileName.cpp

			This program does this and that and something else.

			Written on date by author for purpose

			Other comments as appropriate

		 */
		#include <iostream> // Needed for I/O
		using namespace std;
		int main( void ) {

			// Declare variables to be used.

			// Explain program to user, and get user input.

			// Perform necessary calculations.

			// Echo back the user input and report results.

			return 0;
		} // main

3. List all the variables ( you can think of ) that you will need in your program, and list them right after the int main( void ) line. I like to list my data types in increasing size and complexity, i.e. bools first, then chars, then ints, then floats, then doubles, etc. Leave plenty of room to add additional variables later as you think of them. ( When writing a large program by hand on paper I usually reserve the entire first sheet just for listing variables, and start my code on the next page. ) As you write your program, this variable list can serve as a checklist to make sure you have accounted for everything.

4. Then start outlining the steps needed to solve the problem. If you have worked out a few problems by hand, and better yet written your pseudocode, then you should know what steps need to be done in what order. One good approach here is to go through and fill out a series of comments listing the steps to be performed, ( i.e. type in your pseudocode ), and then go back and put in the computer code corresponding to each comment. Many programs have a basic structure of (1) Read in problem input data, (2) perform necessary calculations, and (3) report results.

5. If a program is large or complex, do not attempt to write the entire thing in one pass. Write part of the code and verify that part is working, and then write some more. For example you might get the input data entry part tested and working before you write any of the code to perform calculations.

6. Evolutionary Development: If a program has special features, don't try to write them all at once either. First write a basic program that can handle simple cases, and then when that is working, add additional functionality, bells, and whistles.

7. If there is a particularly tricky or special piece of code, you may want to test it out first in a smaller test program. Write a small little program that contains only the special code, ( and any supporting code that is necessary to test it. ) You can use this to play around with it and try different things, and when you've got it working right you can then cut-and-paste the code back into your main program.

A highly recommended variation in Matlab is to test commands you're not sure about by typing them directly in the command window, and then once you have figured out what works, drag the working versions from either the command window or the history window into your script. You may have to change some variables around when translating your test commands into your script.

8. Program Testing: Have the program run some test cases for which you know the answer, such as the problems that you have already worked out by hand. Don't forget to test extreme cases ( huge numbers, tiny numbers, zeros, negative numbers, etc. ) as well as simple easy cases. Think about the results your program is giving you and ask if they are correct, ( or at least reasonable, if you don't know the correct answer. )

9. Debugging: If the program runs but gives bad results, there is probably a logic problem somewhere, although there could be other types of errors. The following techniques are often useful:

A. Print out intermediary values, so that you can trace the program's operation.

B. Comment out code that seems to be troublesome, to try and narrow down the problem.

C. Test tricky code with simple cases or in a separate simple program ( see #7 above. )