3.2.5 Writing Maintainable Programs
These are short definitions of key terms for writing maintainable programs.
3.2.5 Writing Maintainable Programs (2)
- Most languages insist that identifiers begin with a letter.
- When declaring variables, programmer should use meaningful identifiers.
- For this reason single letter identifiers should be avoided.
- As spaces are not allowed, use underscores/capitalisation appropriately.
- Use prefixes at the start of indetifier names to remind you of the data type.
3.2.5 Writing Maintainable Programs (3)
When declaring variables/constants, programmer must be aware of scope.
- Local variables are declared and used inside a module/subroutine.
- Global variables are declared at beginning of the code; available throughout.
Using local variables as far as possible - good programming style.
Some programming languages will initialise variables (when they are declared).
- This basically means that the variables are given a starting value.
Generally integers are set to 0, Booleans are set to FALSE and strings are empty.
Constants can make code easier to read and maintain.
- Many programs contain values that don't change whilst the program is running
- These may be universal constants that are always the same.
- New programmers tend to use literals in their code for such values.
- This means that the code contains the actual values spelt out in full.
- As a general rule literals SHOULD BE AVOIDED in code.
- They should be SUBSTITUTED WITH CONSTANTS unless there is a good reason not to.
3.2.5 Writing Maintainable Programs (4)
Modularised code makes the program easier to understand.
- Code should be written as a series of small routines.
- Complex operations should be broken into subroutines (defined separately).
- Modularisation is easier to achieve if top-down techniques have been utilised.
3.2.5 Writing Maintainable Programs (5)
- Most languages allow comments to be inserte.
- These are read by the programmer, and ignored by the computer.
- Comments are vital to making the code understandable.
- They should be written as the code is, not after.
- Subroutines should have comments explaining what it does.
- Variables and constants should have their purpose explained.
- Code itself should use inline comments explaining stages of the algorithm.
- Comments shouldn't however simply repeat the code.
- They must not clutter the program and potentially add confusion.