VRS

Writing Maintainable Programs


What the specification says

Students must be able to:

Define, understand and use the following terms correctly as they apply to programming: variable, constant, identifier, reserved word/keyword

Explain the need for good program-writing techniques to facilitate the ongoing

maintenance of programs

Declare variables and constants, understanding the effect of scope and issues

concerning the choice of identifier (including the need to avoid reserved words/keywords).

Select and use meaningful identifier names and use standard conventions to

show the data types and enhance readability

Use declared constants to improve maintainability

Initialise variables appropriately, before using them

Create appropriately modularised programs (following a top-down/modular

design) making effective use of subroutines to improve maintainability

Annotate the code with comments so that the logic of the solution can be followed

Use indentation and formatting to show clearly the control structures within the code


Key Terms


Why is good programming style important?


Variable Scope

The scope of a variable refers to where it can be used an accessed within a program.


A local variable is declared and used within a module or subroutine, and is only available in that module. It is created when the sub routine is called, and destroyed when the sub routine finishes executing it's code.

The benefits of a local variable are:

The disadvantages of local variables are:


A global variable is declared at the beginning of the program and is available throughout it's running.

The advantages of a global variable are:

The disadvantages of a global variable:


Naming Identifiers

When selecting identifiers (names) for variables, ensure that:


Initialising Variables

When declaring a new variable it must be initialised.

Some languages do this automatically.

Typically booleans = false, integers = 0, strings=null

This clears what was in the data locations before hand

The first time any variable is used, it should be on the left hand side of the assignment operator


Code Layout

Another important factor of good programming technique is well laid out code, this makes it easier to understand for programmers, and reduces the chance of bugs. Factors to consider are:


Modularised code is where code is written as a series of small sub routines, which makes is easier to understand and see what’s going one. This follows closely with top-down design techniques.


Indentation can be done with either tabs or spaces, and it clearly shows nesting constructs and sub-processes. It makes it clear where each statement starts and finishes.


Formatting is where code is grouped together in logical blocks. There should be blank lines in-between, this is like paragraphs in an essay. It shows where each module starts and finishes, and makes swapping them around easier.


Commenting is where the programmer adds short lines of text explaining what the code is doing, which gets ignored by the computer. This should include:

Title


Writing Maintainable Programs.html

Type


web

Download


Back


Switch to New Version