You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Thinking ahead means identifying what is needed before you start solving a problem — the inputs, outputs, pre-conditions, and post-conditions. It also involves considering caching and reusable components to make solutions more efficient. This is a key part of OCR H446 section 2.1.
Before writing any code or designing any algorithm, you must clearly identify:
| Concept | Definition | Example (sorting problem) |
|---|---|---|
| Inputs | The data the system receives | An unsorted list of integers |
| Outputs | The data the system produces | A sorted list of integers |
| Processing | The transformation applied to inputs to produce outputs | A sorting algorithm (e.g., merge sort) |
Why identify inputs and outputs first?
A pre-condition is a condition that must be true before a function, algorithm, or system is executed for it to work correctly.
| Example | Pre-condition |
|---|---|
| Binary search | The data must be sorted |
| Division function | The divisor must not be zero |
| Array access at index i | i must be within bounds (0 <= i < length) |
| User login | The user must have a registered account |
| File read operation | The file must exist and be accessible |
Pre-conditions and defensive programming:
Some programmers handle pre-conditions by checking them inside the function (defensive programming):
FUNCTION divide(a, b)
IF b = 0 THEN
RAISE error "Division by zero"
END IF
RETURN a / b
END FUNCTION
Others document pre-conditions and assume they are met (design by contract):
// Pre-condition: b != 0
FUNCTION divide(a, b)
RETURN a / b
END FUNCTION
Both approaches have trade-offs:
| Approach | Advantage | Disadvantage |
|---|---|---|
| Defensive | Safer — handles invalid input gracefully | Slower — checks run every time, even when unnecessary |
| Design by contract | Faster — no redundant checks | Riskier — if pre-conditions are violated, the result is undefined |
A post-condition is a condition that must be true after a function or algorithm has completed.
| Example | Post-condition |
|---|---|
| Sorting algorithm | The output list is in ascending order and contains the same elements as the input |
| Search function | If the item exists, its position is returned; if not, -1 (or similar) is returned |
| File write operation | The data has been written to disk and the file is closed |
| Bank transfer | The sender's balance has decreased by the transfer amount; the receiver's balance has increased by the same amount |
Post-conditions are used to verify that a function has done its job correctly. They can be checked using assertions in code:
result = sort(myList)
ASSERT result is sorted
ASSERT length(result) = length(myList)
Caching means storing the results of expensive operations so they can be reused without recalculating.
| Concept | Detail |
|---|---|
| Cache | A temporary storage area for frequently accessed data |
| Cache hit | The requested data is found in the cache |
| Cache miss | The requested data is not in the cache and must be fetched/computed from the original source |
| Cache eviction | Removing old data from the cache to make room for new data |
Examples of caching:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.