University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI130 Chap.2 Maintenance, Modularity, Testing and Exceptions

- one subject, every graph, every model, every mark
5 Chapters2-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 12 · COMPSCI130

Maintenance, Modularity, Testing and Exceptions

Define module

The course material gives this chapter a concrete anchor: Official Topic 2 combines maintenance, modularity, testing and exceptions as one software-quality cluster. That module anchor controls how test case is explained and how exception is tested in changed practice.

Maintenance, Modularity, Testing and Exceptions turns module, test case and exception into executable reasoning.

The chapter's practical target is to refactor a fragile function into testable components and design normal, boundary and failure-path checks, so every explanation should connect syntax to program state, control flow and observable output.

Treat module as a precise program object, not a loose label. Identify the value or responsibility of module before execution, then trace what can read it, change it or depend on it.

This makes state changes visible before they become debugging guesses.

Use test case to explain the program's next move. Work through one representative test case input by hand and name the branch, iteration or call that follows.

If the test case trace cannot be stated, the code may run by accident rather than by understood design.

Formula checkpoint

Test pass rate
r=npassnrunr=\frac{n_{pass}}{n_{run}}

The ratio summarises executed test outcomes but does not measure untested behaviour or test quality.

Trace test case

Bring in exception as the test of structure.

Compare normal, boundary and invalid inputs for exception; state the expected behaviour first; then use the mismatch between expectation and result to localise the defect.

For the application — refactor a fragile function into testable components and design normal, boundary and failure-path checks — write the smallest complete example that exposes the rule.

Explain why the exception result works, what would break it and how the program should signal or recover from that failure.

Before running an module example, make a trace table with the important state before and after each operation. Include the value associated with module, the control decision governed by test case and the output or object affected by exception.

The module table turns an unexplained result into a sequence that can be tested one transition at a time.

Test three inputs: an ordinary case, a boundary case and an invalid case. State the expected exception result for each before execution, then compare it with what the program actually does.

A useful test of test case isolates one rule; changing several conditions at once cannot reveal which condition caused the failure.

Test with exception

Practise explaining the solution without reading the code.

For COMPSCI130, name the data representation, the control flow, the responsibility of each function or class and the reason the chosen design supports refactor a fragile function into testable components and design normal, boundary and failure-path checks.

This exception rehearsal matters when a written test or interview asks why the program works rather than whether it produces one correct output.

A complete response should make the task visible before the detail: identify what must be decided, define the relevant terms, connect the evidence to test case, and use exception to test the result.

The final sentence about exception should answer the question actually asked rather than merely repeat the topic.

The controlling limit is specific: Passing selected tests demonstrates those observed behaviours but cannot prove the absence of all defects or justify swallowing unexpected exceptions.

Keep that exception limit beside the worked example, because it separates a careful COMPSCI130 answer from one that sounds confident but claims more than the task or evidence supports.

For revision, retrieve module, test case and exception without notes, explain their relationship aloud, then complete a changed version of the application: refactor a fragile function into testable components and design normal, boundary and failure-path checks.

Record the first failed test case reasoning move and repair it before attempting another case.

In this chapter

What this chapter covers

  • 01

    module

  • 02

    test case

  • 03

    exception

  • 04

    Applying module

  • 05

    Limits of test case and exception

Worked example · free

Repair and test an age parser

Q [8 marks]. The function `def parse_age(text): return int(text)` is used at a form boundary. Specify a contract that accepts decimal strings for ages 0 through 130, refactor the function so invalid syntax and out-of-range values produce `ValueError`, and give expected results for `"20"`, `""`, `"twenty"`, `"-1"` and `"131"`. This is AskSia-authored practice, not a University question or marking scheme.
Figure.Figure. module, test case and exception lead to a chapter-specific maintenance plan.moduletest caseexceptionmaintenance plan
Figure. module, test case and exception lead to a chapter-specific maintenance plan.
  • 1State the accepted input domain and the single outward-facing exception contract.
  • 2Separate integer conversion from the range check in a short implementation.
  • 2Convert Python's conversion failure into the promised `ValueError` without catching unrelated exceptions.
  • 2Give the result or exception for all five named tests, including both range boundaries represented by failures.
  • 1Explain why the tests isolate syntax validation from numeric-domain validation.
One valid implementation is `def parse_age(text):` followed by `try: age = int(text)` / `except (TypeError, ValueError) as exc: raise ValueError('age must be a decimal integer') from exc`; then `if age not in range(0, 131): raise ValueError('age out of range')`; finally `return age`. The input `"20"` returns 20. Empty text and `"twenty"` fail conversion; `"-1"` and `"131"` convert but fail the range check. All four invalid cases expose the same exception type to the caller while preserving distinct messages and causes.
Sia tip — Catch only the conversion exceptions you intend to translate; a blanket `except Exception` can hide defects in the function itself.
Glossary

Key terms

module
A separately named program unit that groups related definitions behind a deliberate interface. Use this definition when the task is to refactor a fragile function into testable components and design normal, boundary and failure-path checks.
test case
A specified input, expected behaviour and observation used to check one aspect of a program. Use this definition when the task is to refactor a fragile function into testable components and design normal, boundary and failure-path checks.
exception
A runtime event that interrupts ordinary control flow and can be propagated or handled explicitly. Use this definition when the task is to refactor a fragile function into testable components and design normal, boundary and failure-path checks.
FAQ

Maintenance, Modularity, Testing and Exceptions FAQ

What is the main task in Maintenance, Modularity, Testing and Exceptions?

Refactor a fragile function into testable components and design normal, boundary and failure-path checks.

How do module and test case work together?

Use module to establish the object or condition, then use test case to explain how it changes the outcome being analysed.

What must a COMPSCI130 answer qualify here?

Passing selected tests demonstrates those observed behaviours but cannot prove the absence of all defects or justify swallowing unexpected exceptions.

How should I revise Maintenance, Modularity, Testing and Exceptions?

Retrieve module, test case and exception, apply them to a changed case, and correct the first point where the evidence no longer supports the conclusion.

Study strategy

Exam move

Reconstruct the relationship among module, test case and exception; complete the chapter application without notes; then test the result against this limit: Passing selected tests demonstrates those observed behaviours but cannot prove the absence of all defects or justify swallowing unexpected exceptions.

Working through Maintenance, Modularity, Testing and Exceptions in COMPSCI130? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI130 Maintenance, Modularity, Testing and Exceptions question and get a clear, step-by-step explanation grounded in how COMPSCI130 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 34 of your subjects at University of Auckland, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMPSCI130 tutor, unlimited, worked the way the exam marks it
The full 2-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works