Maintainability improvements in code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Maintainability improvements in code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function is easier to maintain?
A One huge function that reads input, calculates, prints and saves everything
B Small functions that each do one job, such as calculate_total() and print_receipt()
2. A shop's GST rate of 0.1 is used in 12 places in a program. What is the more maintainable approach?
A Create a constant GST_RATE = 0.1 and use it everywhere
B Type 0.1 in each place it is needed
3. True or false: A class that hides its detail (for example, keeping balance private) helps make code maintainable.
A True
B False
4. Which line uses a constant to make code easier to maintain?
A if score >= 50:
B if score >= PASS_MARK:
5. A bug is found in how a discount is calculated. In which program is it easier to fix?
A One where the discount is worked out in a single small function, apply_discount()
B One where the discount code is copied and pasted into six different places

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1TAX_RATE = 0.1 AA small function that does one job, so it can be fixed or reused without affecting other code
2def calculate_total(items): Bprice = base * (1 + GST_RATE)
3A class can hide its detail by keeping data private, so other code uses methods instead of changing it directly CA constant that stores a value once, so it can be changed in one place instead of many
4Replace a repeated number with a named constant so a future change is made in one place Dself.__balance = 0
5total = cost * 0.1 + fee * 0.1 + tip * 0.1 EThe value 0.1 is repeated (a magic number). If the rate changes, every copy must be found and edited. Store it once as a constant such as RATE.
6def do_everything(): read_input() calc_marks() print_report() FOne big function does many jobs, so a change to one job can break the others. Split it into small functions that each do one job.

Definitions

maintainable code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ total(price, qty): return price * qty
2.
RATE = 0.1 total = price * (1 + ____)
3.
_____ Account: def getBalance(self): return self.balance
4.
class Account: def getBalance(____): return self.balance
5.
PASS = 50 def isPass(mark): ______ mark >= PASS

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A TAX = 0.1
B def add_tax(price)
C return price * (1 + TAX)
D print(add_tax(50))
Question 2
A # Settings in one place
B MAX_USERS == 30
C def can_join(count):
D return count < MAX_USERS
Question 3
A class Account:
B def __init__(self):
C self._balance = 0
D def deposit(amt):
E self._balance += amt
Question 4
A function area(w, h):
B return w * h
C def show_area(w, h):
D print(area(w, h))
Question 5
A 0.1 = GST
B price = 40
C total = price * (1 + GST)
D print(total)

Answer Key

Maintainability improvements in code
Section A: Quick Questions
Q1
B — Small functions that each do one job, such as calculate_total() and print_receipt()
Q2
A — Create a constant GST_RATE = 0.1 and use it everywhere
Q3
A — True
Q4
B — if score >= PASS_MARK:
Q5
A — One where the discount is worked out in a single small function, apply_discount()
Section B: Match the Pairs
Q1
TAX_RATE = 0.1 → A constant that stores a value once, so it can be changed in one place instead of many
Q2
def calculate_total(items): → A small function that does one job, so it can be fixed or reused without affecting other code
Q3
A class can hide its detail by keeping data private, so other code uses methods instead of changing it directly → self.__balance = 0
Q4
Replace a repeated number with a named constant so a future change is made in one place → price = base * (1 + GST_RATE)
Q5
total = cost * 0.1 + fee * 0.1 + tip * 0.1 → The value 0.1 is repeated (a magic number). If the rate changes, every copy must be found and edited. Store it once as a constant such as RATE.
Q6
def do_everything(): read_input() calc_marks() print_report() → One big function does many jobs, so a change to one job can break the others. Split it into small functions that each do one job.
Section C: Definitions
Q1
maintainable code — Code organised so it can be fixed or extended later without breaking other parts, using small functions, classes that hide detail and constants instead of repeated values.
Section D: Fill in the Blanks
Q1
def total(price, qty): return price * qty
Q2
RATE = 0.1 total = price * (1 + RATE)
Q3
class Account: def getBalance(self): return self.balance
Q4
class Account: def getBalance(self): return self.balance
Q5
PASS = 50 def isPass(mark): return mark >= PASS
Section E: Spot the Error
Q1
B — A function header must end with a colon. It should be def add_tax(price): so the small function can hold the tax calculation in one place.
Q2
B — A single = stores the constant's value. == compares values, so MAX_USERS would never be created. It should be MAX_USERS = 30.
Q3
D — Every method in a class needs self as its first parameter so it can reach the hidden data. It should be def deposit(self, amt):.
Q4
A — Python defines a function with the keyword def, not function. It should be def area(w, h): so the small function can be reused.
Q5
A — The name goes on the left and the value on the right. It should be GST = 0.1 so the constant can be reused instead of repeating 0.1.