Maintainability
Maintainability
STARTER
Inexperienced programmers may make these
mistakes with their code:
1. Not using comments.
2. Commenting every line unnecessarily.
3. Inconsistent or no indentation.
4. Repeating code and not using procedures.
5. One character variable names.
6. Too much nesting.
7. Long code lines that are hard to understand.
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A program
that is not
well-
maintained
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A program
that is not
well-
maintained
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A well-
maintained
program
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A well-
maintained
program
Using comments
to divide the
program into
distinct sections.
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A well-
maintained
program
Using comments
to explain what
various parts of
the program are
designed to do.
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A well-
maintained
program
Using sensible,
descriptive
identifier names.
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
A well-
maintained
program
Using
indentation.
SLR2.3 Producing robust programs | Maintainability OCR GCSE (J277)
To make your code as easy to read and maintain as possible, make sure you use:
• Comments to divide the program into sections and explain:
o The program’s purpose.
o Sections of code – typically, selections, iterations and procedures.
o Any unusual approaches taken.
• White space to make program sections easier to see.
• Indentation for every selection and iteration branch.
• Descriptive variable names, explaining each variable’s purpose with a comment when it is declared.
• Procedures and/or functions to:
o Structure code.
o Eliminate duplicate code.
• Constants – declared at the top of the program.
GCSE J277 Unit 2.3 | Producing robust programs Craig’n’Dave
#--------------------------------------------------------------------------
-
Maintainability def gcf_of(factors1,factors2):
#Finds the greatest common factor (gcf) in two input lists
index = 0
The problem with the program below is that it is #Check all the numbers in the factors1 list until the same number is
very difficult to understand what is happening. found in the factors2 list
#Needs the lists to be in numerical order
def gcf(f1,f2): while factors1[index] not in factors2:
x = 0 index = index + 1
while f1[x] not in f2: #Return the highest number found in both lists
x = x + 1 return factors1[index]
return f1[x]
def fcts(x): #--------------------------------------------------------------------------
f = [] -
for c in range(x,0,-1):
if x % c == 0: def factors_of(number):
f.append(c) #Returns a list of all the factors for a number
return f factors = []
x = int(input("Enter a number: ")) #Check all numbers from the number input down to 0
y = int(input("Enter a number: ")) for countdown in range(number,0,-1):
f1 = fcts(x) #If the number divided by the count down has no remainder...
f2 = fcts(y) if number % countdown == 0:
print(gcf(f1,f2)) #...it is a factor and is added to the list
factors.append(countdown)
return factors
Ways in which the second program has been
made more readable: #--------------------------------------------------------------------------
-
The following is a very simple program that asks a student for their first initial, the first three letters of their surname, the year of their birth and their age. It
then concatenates these three variables and outputs the result as a username:
firstInitial = input("Enter your forename initial: ")
surname = input("Enter the first 3 letters of your surname: ")
year = input("Enter the year you where born: ")
age = input("Enter your age in the range 11-19")
Practical Task.
1. Write a program that validates an
email address:
• It must contain an @ symbol.
• Capital letters must be converted to lower
case.
• A dot cannot be the first or last character.
• Double dots are not permitted.