Programming in C - Developing Programs in C
(Page 6 of 9 )
The process of developing programs in C may not be evident if you’ve never written a program before. However, it’s very similar to many other situations in life in which, at the beginning, it just isn’t clear how you’re going to achieve your objective. Normally, you start with an idea of what you want to achieve, but you need to translate this into a more precise specification of what you want. Once you’ve reached this more precise specification, you can work out the series of steps that will lead to your final objective. So having an idea that you want to build a house just isn’t enough. You need to know what kind of house you want, how large it’s going to be, what kinds of materials you have to build it with, and where you want to build it. This kind of detailed planning is also necessary when you want to write a program.
Let’s go through the basic steps that you need to follow when you’re writing a program. The house analogy is a useful one, so we’ll work with it for a while.
Understanding the Problem The first step is to get a clear idea of what you want to do. It would be lunacy to start building your house before you had established what facilities it should provide: how many bedrooms, how many bathrooms, how big it’s going to be, and so on. All these things affect the cost of the house in terms of materials and the work involved in building it. Generally, it comes down to a compromise that best meets your needs within the constraints of the money, the workforce, and the time that’s available for you to complete the project.
It’s the same with developing a program of any size. Even for a relatively straightforward problem, you need to know what kind of input to expect, how the input is to be processed, and what kind of output is required—and how it’s going to look. The input could be entered with the keyboard, but it might also involve data from a disk file or information obtained over a telephone line or a network. The output could simply be displayed on the screen, or it could be printed; perhaps it might involve updating a data file on disk. For more complex programs, you’ll need to look at many more aspects of what the program is going to do. A clear definition of the problem that your program is going to solve is an absolutely essential part of understanding the resources and effort needed for the creation of a finished product. Considering these details also forces you to establish whether or not the project is actually feasible.
Detailed Design To get the house built, you’ll need detailed plans. These plans enable the construction workers to do their job and the plans describe in detail how the house will go together—all the dimensions, the materials to use, and so on. You’ll also need a plan of what is to be done and when. For example, you’ll want the foundation dug before the walls are built, so the plan must involve segmenting the work into manageable units to be performed in a logical sequence.
It’s the same with a program. You’ll need to specify what the program does by dividing it into a set of well-defined and manageable chunks that are reasonably self-contained. You’ll also need to detail the way in which these chunks connect, as well as what information each chunk will need when it executes. This will enable you to develop the logic of each chunk, relatively independently from the rest of the program. If you treat a large program as one huge process that you try to code as a single chunk, chances are that you’ll never get it to work.
Implementation Given the detailed design of a house, the work can start. Each group of construction workers will need to complete its part of the project at the right time. Each stage will
16 need to be inspected to check that it’s been done properly before the next stage begins. Omitting these checks could easily result in the whole house collapsing.
Of course, if a program is large, you’ll write the source code one unit at a time. As one part is completed, you can write the code for the next. Each part will be based on the detailed design specifications, and you’ll verify that each piece works, as far as you can, before proceeding to the next. In this way, you’ll gradually progress to a fully working program that does everything you originally intended.
Testing The house is complete, but there are a lot of things that need to be tested: the drainage, the water and electricity supplies, the heating, and so on. Any one of these areas can have problems that the contractors need to go back and fix. This is sometimes an iterative process, in which problems with one aspect of the house can be the cause of things going wrong somewhere else.
The mechanism with a program is similar. Each of your program modules—the pieces that make up your program—will need to be tested individually. When they don’t work properly, you need to debug them. Debugging is the process of finding and correcting errors in your program. This term is said to have originated in the days when finding the errors in a program involved tracing where the information went and how it was processed by using the circuit diagram for the computer. The story goes that it was discovered how a computer program error was caused by an insect shorting part of the circuit in the computer. The problem was caused by a bug. Subsequently, the term bug was used to refer to any error in a program.
With a simple program, you can often find an error simply by inspecting the code. In general, though, the process of debugging usually involves adding extra program code to produce output that will enable you to check what the sequence of events is and what intermediate values are produced in a program. With a large program, you’ll also need to test the program modules in combination because, although the individual modules may work, there’s no guarantee that they’ll work together! The jargon for this phase of program development is integration testing.
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Functions and Modular Programming >>
More Code Examples Articles
More By Apress Publishing