University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.4 The Function Design Recipe and Testing

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 10 · CSC108H1

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.

In this chapter

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

Q [3 marks]. AskSia-authored practice. A student writes the body of minutes_in(hours, minutes) first and then adds examples copied from what the body returns. Explain which recipe steps went wrong and give the correct order with one example call. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 4.1.Figure 4.1. The five boxes run left to right in the order the recipe prescribes, and each names what that step adds to the docstring or file. The dashed path shows the only loop: a test that does not match sends you back to the body, never back to change the examples to fit the code.1 Examplescalls and results2 Headername, types3 DescriptionReturn ...4 Bodythe code5 Testcompare outputmismatch: fix body
Figure 4.1. The five boxes run left to right in the order the recipe prescribes, and each names what that step adds to the docstring or file. The dashed path shows the only loop: a test that does not match sends you back to the body, never back to change the examples to fit the code.
  • 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.
The recipe runs examples, header, description, body, test. Copying examples from the body turns the test into a check that the code agrees with itself, so a wrong body can never fail. Writing minutes_in(1, 30) as 90 before the body exists is what lets step five catch a mistake.
Sia tip — If a test fails, change the body, not the example; an example rewritten to match the output can no longer find any bug.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 tutor, unlimited, worked the way the exam marks it
The full 4-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