1.7: Elements of Style

Individual writers, even whole magazines or journals, have their own writing style. Often a glimpse at a few paragraphs can give clues to the author or the type of publication. Still, there are general rules to follow for acceptable style; entire books have been devoted to this subject. Style will always be in style.

Dress, carriage, and response are parts of personal style, as are straightness of teeth, polished or scuffed shoes, and a fast or slow gait. Program style is determined by overall appearance as well as by details of implementation. We want to see the logical structure of a program clearly; too many details cloud the picture. Data abstraction and functional modularization are tools for sharpening the picture. Hiding information, so that only parts of a program that need it can see it, also provides better focus. People react differently to unexpected situations: some become erratic while others remain calm, going cheerfully about their business. Programs should react to unexpected data with aplomb, letting us know about errors, but getting on with the job when possible. Details of a program should not affect its logical structure, only its efficiency. Details can make an otherwise stylish program sloppy, slow, and in need of repair, or they can finish the design so it is elegant, sleek, and well-tuned. What we see isn't always what we get, but stylish programs suggest a better thought-out design and product.

Since good programs must be written with style, guidelines are offered here for this purpose. A good program is correct, easy to read, and changeable through a reasonable amount of effort. Whether or not it should be efficient depends on the application. In some cases an efficient program may be written with the same effort as an inefficient one. In other cases it will be necessary to put in more effort to achieve efficiency, so the additional programming may be done only if it will bring substantial (or required) savings in computation time.

1.7.1 Expressing Purpose

1.7.2 Communication between Functions

1.7.3 Ensuring against Data Errors: Defensive Programming

1.7.4 Changing Programs

1.7.5 Verification and Debugging

1.7.6 In a Nutshell