A computer program is a set of instructions to the computer. While the instructions are to be executed by the computer, the language in which we communicate these instructions is meant to be comprehensible to people in their direct form and are intended for machine use only indirectly, through the process of compilation. Therefore, a basic principle of good programming is that the program should be easily understood.
A program that can be understood can also be analyzed and tested for correctness. Incorrect programs have plagued computing from its early days. Another basic principle of good programming then is to write correct programs. In a more theoretical vein, we talk of proof of program correctness as a reflection of efforts to prove in logical and unambiguous terms that a piece of program code is correct i.e., it does what it is intended to do and nothing else. Theoretical progress has been made only in very limited contexts, and at present we cannot rely on a formal theory to guide us to produce correct programs. Instead, we rely on a number of rules and guidelines that are closer to common sense than they are to a formal theory.
Every program has a structure. If the structure is well-defined, the program is correct and easy to understand and modify. On the other hand, a poorly structured program may have errors that are difficult to detect, maybe even, hard to read, and troublesome to modify. In cases where a change has to be made, it may become necessary to scrap the whole program and start afresh with a new one. In contrast, a structured program is characterized by clarity and simplicity in its logical flow structure. It reads like ordinary language from the beginning to the end, instead of branching from later paragraphs to earlier ones and back again. So, a simple, straight flow of logic is another principle of good programming.
Programming by itself is a complex task and the aim of structured programming is to reduce and control complexity as it is not possible to eliminate the complexity completely. Human intellect can comprehend only a limited amount of data at a given time. When the human mind is faced with a complex task, it tries to manage it by breaking it into a series of well-connected smaller parts. Each smaller part of the module, as it is called, is simple and comprehensible by itself so that within each module, all simplicity and clarity are achieved. So one of the characteristics of a well-structured program is that it consists of a large number of small modular programs. Each modular program is self-contained and performs a single function. Each module can be tested and debugged independently.
This means that every module has only one input and one output point, though within the module there may be branches and loops. Of course, the programmer has to use his common sense to divide the complex program into a large number of smaller self-contained units or modules. Since each module has a specific independent function, it can be perfected separately without mixing it with the parts of the program. This is indeed a simplification of the programming art.
How is a modular structure developed? In practice, a modular structure is developed by proceeding from the general to the specific, or by using what is widely known as the top-down approach. Starting with the barest specification of the problem, one goes about filling in the details. Suppose we are to develop a payroll processing problem utilizing the top-down approach. The most abstract statement of the task is “write pay-roll”. Then one can proceed to break down these general more specific statements, such as:
- Edit input data.
- Process against the master file.
- Output payroll checks and other reports.
Each of these broad functions can be subdivided into more specific ones, for example, we can divide the first job still further as follows:
- Check the valid employee number.
- Check time card data against job tickets.
- Check for valid department numbers, and so on.
The first program module which may be called the Read Module performs a certain function after making all checks as given by commands or statements within that module. The main point is that the execution of commands or statements within the module has nothing to do with statements in other modules and that is why we say each module can be independently developed, tested, and debugged. In the overall structure of the program, each module is taken as a single instruction.
If a problem can be divided into, say, five modular functions, the main program can be considered to be made of five dependent instructions, each instruction performing a module. A program organization is, therefore, first divided into modular organizations and then each module is further divided into a statement organization. Just as a government organization performs the governing function by a hierarchy of officials at different levels, a complex programming task is achieved by dividing the job among different modules organized in a particular hierarchical structure. This is the top-down approach and this seems to work well in all human tasks. The below figure shows what is called the modular level or modular hierarchy diagram of the above problem.
It should be noted that no programming language is used while developing a programming structure. The thoughts are simply written in ordinary language with or without graphic aids such as arrows, boxes, brackets, etc., and the whole function is divided into a large number of simpler functions in a logical way. So, structured programming is not concerned with any particular language. It is concerned with programming in general.