Thursday, March 14, 2013

Program Style and Layout

Program Style and Layout

Program Style and Layout

Programs should be written in such a way that they are:

1 Easy to understand.

2 Well structured.

3 Easy to modify.

Programs written with these points in mind:

I Are easier to debug.

2 Are far less likely to contain errors in the first place.

3 Can be worked on by teams.

4 Can be modified to fit new situations.

A program must be carefully designed and planned . Even then a large program cannot be written successfully by starting at the beginning and working through it instruction by instruction. It should be written top down . The main operations of the program should be worked out first.

1 In a structured language such as COMAL or Pascal these operations can then be refined using procedures .

2 In an unstructured language, as in many versions of BASIC, the program can still be divided up into distinct parts or modules.

To make a program easier to read the programmer can also:

I Include helpful comments in the program.

2 Choose names of variables, procedures and functions which make their purpose clear without further comments.

USE OF COMMENTS

Comments are also known as remarks or narrative.

In BASIC any part of a line after the word REM is assumed to be comment. In COMAL, REM can be used or just //.

Comments can be used;

1 To highlight the modules of the program.

2 To explain the function of modules.

3 To explain the variables used.

Examples of the use of comments in BASIC

1 100 REM *******************************************************

110 REM ***** IDENTIFICATION MODULE

120 REM ************ *********************************** ********

130 REM HOUSE PURCHASE PROGRAM

140 REM VERSION 5 15/1/87

2 500 LET high =0 : REM high is highest mark so far

510 LET pnum = K-1 : REM there are K-1 names

USE OF HELPFUL IDENTIFIERS

The name given to a variable (or a file, etc.) can be chosen in such a way that anyone reading the program can see what the variable represents from its name. Some languages, including some versions of BASIC, make this difficult because they only allow short names.

Example in BASIC

Part of a program to calculate the area of a triangle:

100 INPUT BASE,HEIGHT

110 LET AREATR=0.5*BASE*HEIGHT

Even if only one letter is allowed for names, the following would still be of some help. 100 INPUT B,H

110 LET A=0.5*B*H

PROGRAM LAYOUT

Where the language allows procedures, programs should be set out to show top down structure (as in the two short COMAL programs .

Even in BASIC you could still use the COSUB statement to produce a similar effect. In any case try to keep to the following rules:

1 Break the program up into modules. Usually the modules would match the boxes in the outline program flowchart.

2 Keep branching between modules to a minimum. In BASIC this is difficult, but GOTO should be used sparingly and subroutines should each have only one exit point.

3 Set the statements out in a clear manner, for example, spaces can be used to separate words and to show up loops.

Example

The following is a section of a program which reads a table of values and column headings and prints the table out.

100 REM ********************** ***** ****************************

110 REM ***** INITIALISE VARIABLES,DIMENSION ARRAYS ****

120 REM *******************************************************

130 LET NCOL=5 :REM NUMBER OF COLUMNS

140 LET NROW=10: REM NUMBER OF ROWS

150 DIM HEAD$(NCOL)

160 DIM TABLE$(NROW,NCOL)

200 REM *******************************************************

210 REM ***** READHEADINGSANDTABLE *****

220 REM *******************************************************

230 FOR I = 1 TO NCOL

240 READ HEAD$(I)

250 FOR J = 1 TO NROW

260 READ TABLE(J,I)

270 NEXT J

280 NEXT I

300 REM *******************************************************

310 REM ***** PRINT HEADINGS ******

320 REM *******************************************************

etc.

Notes:

1 Lines 240 to 270 have been indented (i.e. moved in from the side of the page) to highlight the fact that this is a loop within a loop.

2 The same result could have been achieved with a program section written as follows (in many versions of BASIC):

10 DIMA$(5),B(10,5)

20 FORI = lT05,READA$(I),FORJ = lTo10,READB(J,I),NEXTJ,NEXTI

It is true that the longer version would:

(a) Take a greater time to type on a keyboard.

(b) Take up more store in its source form.

(c) Take more time to load into the computer or to print. But the longer version is;

(a) Easier to understand.

(b) Easier to adapt.