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
Variable – the name associated with a location in memory which may hold different values. The value of the variable may change during the running of the program. It enables a program to be written before the values are known.
Constant - a value that, once set, cannot be changed while the program is running. It may be used literally, for example PI = 3.141
Reserved Word – a word that is defined in a programming language. It can only be used for the purpose specified by the language. Typical reserved words are FOR, NEXT, LOOP, integer and so on.
Why is good programming style important?
Allows the programmer to focus on what the program is intended to do, as it will be significantly easier to see if it is doing it.
Allows other programmers to read and understand it easily – this is important as programmers often work in teams
If the code needs to be revisited in the future, it is quicker and easier to become re-familiarised with it's written well.
It will also make it easier to check for errors and find bugs
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:
They can not be affected by other parts of the program
This makes finding bugs easier, and testing sub routines
Identifiers can be reused
The disadvantages of local variables are:
They are destroyed when the sub-routine is finished executing
They are therefore only accessible within the module they were declared in
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:
There is no need to pass their value as an argument or parameter to a sub routine, because it can be accessed globally by everything
Sub-routines can change or read the value directly
The disadvantages of a global variable:
A sub-routine can accidentally overwrite a global variable
Makes debugging harder
Not good use of memory
Should only be used when necessary
Naming Identifiers
When selecting identifiers (names) for variables, ensure that:
Use meaningful identifiers
Make division between words with either camelCase or under_scores
Sometimes prefixes relating to data type start the variable name, for example intAge.
Don't use reserved words
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:
Program Header – title, who wrote it...
Sub-routine header – what the module does
Declaration of variables and constants – what that variable is for
The code it's self