Caution: On Program Style




Document: Software Engineering 1: Course Notes

next Chapter 2 pp. 11-19
up Chapter 1 pp. 1-10
previous Watch for the new technical terms!

Caution: On Program Style

In dissecting the initial example program Alcock comments that:

"You have complete freedom of layout. Statements may be typed several to a line, one per line, one to several lines."

His point here is that as far as the computer - or more precisely, the C compiler - is concerned, many details of layout or formatting of the program are irrelevant. This is true. However, you should notice that programs have effectively two parallel, but quite distinct, purposes:

  1. To represent a suitable sequence of instructions which (after compilation) the computer can execute to accomplish some task. This is the role which Alcock explicitly recognises and discusses.
  2. To document that set of instructions so that some person can understand how the computer is accomplishing the task. This becomes vitally important whenever it is necessary to modify or extend the behaviour of a program - or to correct previously undetected malfunctions. Alcock does not explicitly refer to this purpose of the program at all.

It should be clear that, in its role as documentation for another human being, the details of layout and formatting of a program are very important. A poorly formatted program may be perfectly "correct" - in the sense of generating the correct answers to the problem at hand - but may be virtually impossible for a human being (even the original author) to decipher if it becomes necessary to enhance it (or even to decide whether it may malfunction in certain circumstances).

Another aspects of program design which is similarly constrained is the choice of identifiers - names for variables and other objects which the program uses. The computer is largely insensitive to particular identifiers, as long as, in each context, they unambiguously identify the "correct" object. In particular, the computer does not care whether an identifier is "meaningful" in human terms. The names P, Principal and bZ34ywT are all equally valid to the machine, as along as they are used consistently (unambiguously). However, the net effect if entirely different from the point of view of someone trying to understand how the program works. So try to get into the habit, from the very start, of taking care with the formatting and layout of your programs, and with your choice of identifiers. These things may not directly affect the functioning of your program, but they will seriously affect how long it actually takes you to get the program working satisfactorily!




Document: Software Engineering 1: Course Notes

next Chapter 2 pp. 11-19
up Chapter 1 pp. 1-10
previous Watch for the new technical terms!



McMullin@ugmail.eeng.dcu.ie
Wed Apr 12 19:40:14 BST 1995