The Programming Process

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > What is Programming?


Introduction

Just like the writing process for conventional writing, there is a process for writing good programs. This section will go through the process of writing programs, from defining the problem to designing and writing the program to solve the problem. Programming, simply put, is the act of writing computer programs. Programs are essentially a series of instructions written down for a computer to follow.

Writing instructions is a deceptively difficult task. People are very good at intuitively breaking down tasks into simple steps. Someone can tell you "go buy some bread", and you immediately form a series of steps without thinking of them:

  1. Go upstairs and get your wallet out of your sock drawer
  2. Put your wallet into your pocket
  3. Go back downstairs and get the car keys
  4. Drive to the store and park in the parking lot
  5. Walk to the bread aisle in the store
  6. Choose a loaf and pay for it
  7. Drive back home

Some of these steps can even be broken down into smaller, more detailed steps. How do you walk up a flight of stairs? How do you drive a car? There is a near-infinite series of steps that you could think of to do a task as seemingly simple as buying a loaf of bread.

Computer Science attempts to find solutions to solve problems. Some problems are relatively simple--we'll start with a program that will just print out a greeting. But as you progress in computer science the programs become more complex. The good news is that all programs can be written by following a certain procedure. This chapter will give you a brief introduction to writing instructions for computers. You'll use this process for almost every program you write.

The Steps

The process for writing good programs can be broken down into the following steps:

  1. Define the problem
  2. Determine a solution
  3. Pseudocode (Write out the instructions)
  4. Write the program using a programming language
  5. Test the program

In this section we will discuss each of these steps in detail so that you can apply them to any programs you write for your course work or future jobs.

Define The Problem

All programs are written to solve one or more problems. The first and most important step in writing a program is ensuring that the problem is well-defined. Ask yourself, "What is this program supposed to do?" Ensuring that you have a good idea what you want your program to do makes the process of writing the code much easier.

For example, let's say you want to write a program that determines the litres of water in a pool. The purpose of the program will be to ask the user to input the dimensions of the pool before calculating and outputting the volume of the pool.


Determining How To Solve The Problem

Once you have the problem defined the next step is to determine how to solve it. Think of a program as a set of instructions the computer has to follow. Every step should be as simple as possible. Computers don't actually know how to do anything; you need to give them step-by-step instructions. The simpler the steps the easier they are to write down and code.

But before you sit down and write any code, you should decide how the program should go about solving the problem(s) presented to you. Important concerns to take into consideration at this point are:

  • What inputs will the program be provided with?
  • What kind of data processing will the program have to do to solve the problem?
  • What outputs are the users expecting?


Do not sit in front of the computer and start writing the code. You may wish to sort out what is to be done by writing it on paper first. This is very helpful as it makes you plan and write the code step by step. Also, you'll have to draw on problem solving strategies. In the above example, you'll use math skills to know that volume = length x width x height. Other programs will require more complex problem solving strategies. Some of you are from particular programs where you have specialized knowledge and you're learning how to write programs so you can use your specialized knowledge to solve problems using computers.

Pseudocode

A useful technique when preparing to write any program is to first pseudo-code it. Write down the processing your program will perform in an English style syntax. Pseudo-code can help you identify how many of what types of variables you will need as well as any control structures that may be necessary.

To continue with our previous example, the pseodocode to find the volume of a pool might look something like this:

  1. Prompt user to enter height, then width, then height of pool
  2. Multiply height by width and height
  3. Output pool volume. Remember to put unit of measurement (gallons? litres?)
Photo credit: original photo from Flickr user lucathegalga at http://http://www.flickr.com/photos/lucathegalga

Of course this is a very simple example. Any real-life program would likely have more details. For instance, you might write a program that also calculates the water flow per second and then sends a trigger to turn off a water pump when the pool is filled to the percentage specified by the user.

To give another example, Here is pseudo-code for a simple program that solves the problem of counting the number of times the number 7 appears in a student number.

  1. Ask user to input student number
  2. Somehow isolate each digit of the student number (how? if a student number has 7 characters-6775433 then I could divide by 100000 to get the first digit, 6. Then to get the next number I'd have to divide by 10000. I could keep going until I had all the digits isolated.)
  3. Check each isolated digit to see if it is equal to 7. If it's equal increase a counter value to keep track of the number of 7's.
  4. Output the number of 7's.

You'll see how the interesting part of the problem is to isolate each digit. If you've coded before you'll also know you could solve this problem using the % (modulus) operator. You could also use String methods. The point is that there are often many ways of solving a problem. Some solutions are more elegant than others. And sometimes solutions are just a matter of personal approach.

In that second example you also see that using sample data is helpful when problem-solving. Using a made-up student number-6775433-helps to see different ways of solving the problem. Use sample data as you write out your ideas.

Also, once you think you've found a solution, make up some test data to confirm your solution works.

In summary, pseudocode requires you to think about and write down the steps to solve a problem in a language you know, rather than in any programming language. Pseudo-code is designed to quickly convey the general idea of what the program should do, without getting bogged down in syntax and specific coding details. It is essentially a half-English, half-code language used by programmers to get their ideas down on paper quickly. As you get more familiar with coding your pseduocode will start to look more like a programming language.

Write the Program using a Programming Language

Once you have identified the nature of the problem, figured out how to solve it, and written down the steps in pseudocode, the next step is to actually code it using a language the computer can understand.

This course will teach you how to use the programming language Java. There are many other programming languages. The good news is that once you know how to program in one language it's not very difficult to learn additional programming languages. Java is a popular programming language, and so are programming languages named C, C++, and Visual Basic.

To give you an example of how a program looks, here is the Java code for a program that will find the litres of water in a pool:

import javax.swing.*;
public class PoolVolume {
 public static void main(String[] args) {
  double length;
  double width;
  double depth;
  double volume=0;
  String input;
  
  input=JOptionPane.showInputDialog("What is the length of the pool (in meters)?");
  length=Double.parseDouble(input);
  
  input=JOptionPane.showInputDialog("What is the width of the pool (in meters)?");
  width=Double.parseDouble(input);
  
  input=JOptionPane.showInputDialog("What is the depth of the pool (in meters)?");
  depth=Double.parseDouble(input);
  
  volume=length*width*depth;
  
  System.out.println("Pool volume is " + volume + " meters squared which is " + volume*1000 + " litres");
 }//close main
}//close class

You'll notice this program a blend of English words, and some specialized names. After about a week into this course this program will look very simple, even if you have never programmed before!

Test the Program

For a program of any length it is best to start off coding one small part at a time and testing it to assure it works before coding the whole thing and trying to track down the errors. Once your program is coded, you can run it through the compiler to see if you have made any syntactical errors. After the program is up and running, it must be tested to ensure it produces the correct results. Tracking down and removing faults from a program is referred to as "debugging".

When you test a program you'll first correct any mistakes so your program compiles. This process can be frustrating but in time you'll get good at finding what's the mistakes in your code. It might be something as simple as capitalizing a letter that shouldn't be capitalized!

For example, the first time I tried to compile PoolVolume.java I realized I had missed the last 'n' in JOptionPane.

Once your program compiles you'll have to test it to make sure there are no logical errors in the code. For example, maybe you forgot to convert cubic metres in litres. Then when you ran the program you'd notice that the number of litres was ridiculously tiny!

Errors that result in obvious errors are quite easy to locate (even if they're not easy to solve). Professional computer scientists must work very carefully to write code using practices that result in as few errors as possible. The most difficult errors to fix are ones that are difficult to notice. There are several tragic examples, where computer errors resulted in injury and death.

Previous Page: What is Programming? Next Page: Anatomy of a Program