CSC108H1 Chap.4 The Function Design Recipe and Testing
The Function Design Recipe and Testing
Five steps in a fixed order
The Function Design Recipe asks for examples first, then the header with its type contract, then a description, then the body, and finally a test of every example in the shell.
The course uses it in every assignment because it documents a function for other readers and plans the work before any code is written.
Descriptions and preconditions
A good description is a command such as Return the total, says what the function does rather than how, and names every parameter.
A precondition writes down what the function takes for granted about its inputs beyond their types, and meeting it is the caller's job.
How the recipe is assessed
Assignment 1 gave separate tasks for following the whole recipe from a short specification, for completing the docstring examples of a provided function, and for debugging a provided function by adding one example it passes and one it fails.
The handouts mark coding style and correctness, and correctness, measured by tests run after the deadline, is the largest single portion of an assignment mark.
What this chapter covers
- 01
The five steps of the Function Design Recipe
- 02
Writing docstring examples before code
- 03
Type contracts in the function header
- 04
Rules for a strong docstring description
- 05
Preconditions and who must meet them
- 06
Testing examples and corner cases in the shell
Worked example · free
Put the recipe steps in order for a conversion
- 1Examples come first, so they record the intended answer, such as minutes_in(1, 30) giving 90.
- 1The header and type contract follow, then a description naming both parameters.
- 1The body comes fourth, and testing compares it with the examples, never the reverse.
Key terms
- Function Design Recipe
- The course's five-step process for writing a function: examples, header, description, body and test.
- Type Contract
- The parameter and return types written in a function header.
- Docstring
- The triple-quoted text after a header that describes a function and shows example calls.
- Precondition
- A condition on the inputs, recorded in the docstring, that every caller must satisfy.
- Corner Case
- An unusual input, such as zero or an empty string, used to test a body beyond its standard case.
- Basics Checker
- The assignment tool that checks style and types before submission but not full correctness.
The Function Design Recipe and Testing FAQ
What order do the Function Design Recipe steps go in?
Examples, header, description, body and test. Writing examples first fixes what the function should return before you think about code, and the final step runs those same examples in the shell to check the body against them.
How should a docstring description be written?
As a command that starts with a verb such as Return, saying what the function does rather than how, naming every parameter and stating what is returned. The posted worksheet solution lists these rules explicitly.
Does passing the assignment checker mean full marks?
No. The handouts state that the basics checker covers style and types, and that further tests run after the deadline decide correctness, which is the largest single part of the mark. A file with a syntax error that stops it from running can score zero.
What is a precondition in a docstring?
It is a condition on the inputs that the type contract cannot express, such as a count being greater than zero. The function author writes it in the docstring, the caller is responsible for meeting it, and a call that breaks it gets no promise.
Why write examples before the body?
Because examples written first record what the function ought to return, independent of any code. If they are written after the body, they tend to copy whatever the body produces, so a wrong body can never fail its own tests in the final recipe step.
Exam move
Take three functions from the worksheets and redo them strictly in recipe order, timing how long each step takes. For each, write one corner-case example beyond the docstring and test it. Then swap descriptions with a classmate and check each one against the four description rules, rewriting any that describe how instead of what. Keep a list of the corner cases you choose, by parameter type, to reuse in later weeks.
Working through The Function Design Recipe and Testing in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 The Function Design Recipe and Testing question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.