2.1.3 Thinking Procedurally
2.1.3(a) Identify the Components of a Problem
Top-Down Design / Stepwise Refinement
| Step | What to do |
|---|---|
| 1 | Identify major components of the problem |
| 2 | Break each component into subcomponents |
| 3 | Continue refining until tasks are small enough to implement |
| 4 | Repeat/iterate (top-down design / stepwise refinement) |
Overall problem: Create an adventure game.
| Level | Component | Subcomponents/examples |
|---|---|---|
| 1 | Characters | Player, NPCs, enemies |
| 1 | Environments | Level design, backgrounds, interactive objects, collision |
| 1 | Game mechanics | Combat, scoring, quests, progression |
| 1 | User interface | Menus, HUD, inventory screen |
| 2 (Characters) | Player character | Appearance, stats, inventory |
| 2 (Characters) | NPCs | Dialogue, quest logic |
| 2 (Characters) | Enemies | AI behaviour, combat stats |
2.1.3(b) Identify the Components of a Solution to a Problem
| Component | Purpose | Example routine |
|---|---|---|
| User identification | Confirm who is making reservation | getUserName() |
| Book selection | Find the target book record | getBookISBN(bookTitle) |
| Availability checking | Ensure copies are in stock | checkAvailability(ISBN) |
| Collection location | Let user choose pickup branch | selectLocation() |
| Reservation confirmation | Save reservation and confirm | confirmReservation(userID, ISBN, location) |
function getUserName():
if userIsSignedIn() then
return getNameFromDatabase()
else
promptLoginOrRegister()
return null
end if
end function
function getBookISBN(bookTitle):
bookRecord = searchDatabase(bookTitle)
return bookRecord.ISBN
end function
function checkAvailability(ISBN):
stockCount = queryDatabase("SELECT stock FROM books WHERE isbn = " + ISBN)
return stockCount > 0
end function
function selectLocation():
displayMenu(["Main Branch", "North Branch", "East Branch"])
return getUserSelection()
end function
procedure confirmReservation(userID, ISBN, location):
writeToDatabase(userID, ISBN, location, currentDate())
displayConfirmation()
end procedure
2.1.3(c) Determine the Order of the Steps Needed to Solve a Problem
| Sequencing rule | Why it matters |
|---|---|
| Respect dependencies | Some tasks require earlier outputs (e.g. ISBN before stock check) |
| Parallelise where possible | Independent tasks can run simultaneously |
| Validate input early | Prevents invalid data flowing through later stages |
| Authenticate before privileged actions | Protects security and user-specific operations |
// Sequential steps (dependencies exist)
procedure makeReservation():
// Step 1: User must sign in first
userID = authenticateUser()
if userID is null then
print("Please sign in")
return
end if
// Step 2: Get book details
bookTitle = input("Enter book title: ")
ISBN = getBookISBN(bookTitle)
// Step 3: Check availability (depends on ISBN)
if NOT checkAvailability(ISBN) then
print("Book not available")
return
end if
// Step 4: Select location (depends on book being available)
location = selectLocation()
// Step 5: Confirm (depends on all previous steps)
confirmReservation(userID, ISBN, location)
end procedure
| Step order | Dependency reason |
|---|---|
| User auth → Book selection | Must know who is reserving before creating reservation |
| Book selection → Availability check | Need ISBN/book first to check stock |
| Availability check → Location | Only ask for pickup location if book is available |
| Location → Confirmation | Final record requires all previous values |
• Checking availability before selecting a book fails
• Confirming before authentication creates security risks
2.1.3(d) Identify Sub-Procedures Necessary to Solve a Problem
Benefits of Sub-Procedures
| Advantage | Practical benefit |
|---|---|
| Modularity | Components can be built independently |
| Reusability | Same routine can be reused across features |
| Testability | Each routine can be unit tested in isolation |
| Maintainability | Fix once in one procedure, benefit everywhere |
| Readability | Smaller named routines make code easier to follow |
| Team collaboration | Different developers can own different procedures |
// Sub-procedure 1: Get user name
function getName():
if isSignedIn() then
return currentUser.name
else
return null
end if
end function
// Sub-procedure 2: Get book ISBN
function getISBN(bookTitle):
book = database.findBook(bookTitle)
return book.ISBN
end function
// Sub-procedure 3: Check availability
function checkAvailability(ISBN):
return database.getStock(ISBN) > 0
end function
// Sub-procedure 4: Select location
function selectLocation():
return displayLocationMenu()
end function
// Sub-procedure 5: Confirm reservation
procedure confirmReservation(userID, ISBN, location):
database.createReservation(userID, ISBN, location)
end procedure
// Main procedure combining all sub-procedures
procedure main():
name = getName()
ISBN = getISBN(input("Book title: "))
if checkAvailability(ISBN) then
location = selectLocation()
confirmReservation(name, ISBN, location)
end if
end procedure
• Be able to decompose a problem into components
• Identify appropriate sub-procedures for given scenarios
• Understand the order/sequence of steps
• Recognise dependencies between procedures
• Explain benefits of decomposition (modularity, testing, teamwork)
• Write pseudocode for sub-procedures with clear inputs/outputs