2.1.2 Thinking Ahead
2.1.2(a) Identify the Inputs and Outputs for a Given Situation
Understanding Inputs and Outputs
| Category | What it means | Key design checks |
|---|---|---|
| Inputs | Data provided to the system for processing | Data type, format/order, validation rules |
| Outputs | Results produced after processing | Clarity, destination (screen/file/device), usefulness |
| Inputs | Purpose |
|---|---|
| transactionType | Deposit / balance check / withdrawal selection |
| cardDetails | Identify account via card reader |
| pin | Authenticate user |
| amount | Amount to deposit or withdraw |
| Outputs | Purpose |
| --- | --- |
| displayBalance | Show account balance on screen |
| dispenseCash | Provide physical cash |
| printReceipt | Provide transaction record |
| audioFeedback | Accessibility and confirmation prompts |
2.1.2(b) Determine the Preconditions for Devising a Solution to a Problem
Purpose of Preconditions
| Purpose of preconditions | Benefit |
|---|---|
| Error prevention | Blocks invalid states/data before execution |
| Documentation | Makes function assumptions explicit to other developers |
| Efficiency | Avoids unnecessary defensive checks deeper in code |
function pop(stack):
// PRECONDITION: Stack is not empty
if isEmpty(stack) then
return ERROR("Cannot pop from empty stack")
end if
item = stack[top]
top = top - 1
return item
end function
Without preconditions: The function might crash or return garbage data when popping from an empty stack.
With preconditions: The function handles the error gracefully, returning a meaningful error message.
2.1.2(c) The Nature, Benefits, and Drawbacks of Caching
Benefits of Caching
| Caching benefit | Why it helps |
|---|---|
| Speed | Cache access is far faster than secondary storage access |
| Efficiency | Reuses frequent data instead of repeatedly fetching from slow storage |
| Reduced load | Lowers pressure on databases/disks and network resources |
Drawbacks of Caching
| Caching drawback | Why it is a problem |
|---|---|
| Cache size limits | Small caches cannot hold all useful data; very large caches add lookup overhead |
| Implementation complexity | Cache policies/prefetching are hard to tune correctly |
| Data consistency risk | Cached data can become stale if source data changes |
| Cache misses | Misses force slow fallback to main/secondary storage |
| Step | What happens |
|---|---|
| 1 | User visits www.example.com |
| 2 | Browser downloads HTML/CSS/images |
| 3 | Browser stores these files in local cache |
| 4 | User revisits the page |
| 5 | Browser serves cached files (if valid) instead of re-downloading |
Potential problem: stale content may be shown until cache expiry/refresh.
2.1.2(d) The Need for Reusable Program Components
Advantages of Reusable Components
| Advantage of reuse | Practical effect |
|---|---|
| Time/cost efficiency | Faster development with less duplicated work |
| Reliability | Reused components are often already tested |
| Consistency | Same behaviour across multiple modules/projects |
| Maintainability | One fix can improve all places where component is used |
Challenges
| Reuse challenge | Practical impact |
|---|---|
| Compatibility issues | Integration can fail across different environments/frameworks |
| Modification costs | Adapting third-party code may be expensive/complex |
| Learning curve | Team needs time and documentation to use components correctly |
// Reusable sorting function in a library
function quickSort(array, compareFunction):
// Implementation of quicksort algorithm
// Thoroughly tested and optimised
end function
// Used in Program A: Sorting student grades
grades = [85, 92, 78, 95, 88]
sortedGrades = quickSort(grades, descending)
// Used in Program B: Sorting customer names
customers = ["Smith", "Jones", "Brown", "Taylor"]
sortedCustomers = quickSort(customers, alphabetical)
// Used in Program C: Sorting products by price
products = [{name: "Book", price: 10}, {name: "Pen", price: 2}]
sortedProducts = quickSort(products, byPrice)
Benefit: The quickSort function is written once, tested thoroughly, and reused across multiple programs without rewriting or retesting.
• Be able to identify inputs and outputs for given scenarios
• Understand the purpose of preconditions and why they matter
• Explain benefits AND drawbacks of caching (don't just list benefits)
• Describe advantages and challenges of reusable components
• Give specific examples for each concept (ATM, stack, web browser, sorting)
• Consider data types appropriate for different inputs