DoAssignment study guide

C1.4 · Design reusable modules and methods

Learn to design reusable modules and methods through clear examples and targeted practice.

Ontario Grade 12 Computer Science

Designing Modular Programs

ICS4U C1.4 · Give shared tasks a clear purpose and interface

In ICS3U, you learned to organize instructions, use variables, and call procedures or functions. C1.4 builds on that foundation: design methods and modules so useful tasks can be called where needed and their purpose is clear to another programmer. A reusable part is not simply code moved elsewhere. Its responsibility, inputs, expected result, and assumptions should fit together. This lesson uses short Python 3 examples; the design ideas apply even if your class uses another language.

What you will learn

  • Explain how methods and modules can support reuse.
  • Design a method with a clear responsibility, parameters, preconditions, and expected result.
  • Choose parameters that allow one method to handle relevant changes in input.
  • Trace a method call and describe its expected result without assuming the code was run.

1. From a repeated task to a reusable method

A method is a named group of instructions designed for a particular task. A program can call it whenever that task is needed. A module is a separate, named unit of program code that can contain related definitions. Designing reusable parts means deciding what belongs together and what information a method should receive.
Suppose several parts of a program need the same welcome message. Duplicating its instructions means a later change might be made in one place but missed elsewhere. A method provides one definition that each part can call. The benefit is a shared, understandable task—not the idea that every short instruction must become a method.
Before writing a method, describe its responsibility in one sentence. Identify which information may vary between calls and make that information a parameter. Decide what the caller needs afterward. Return a value when the caller needs to use a computed result; make an action such as displaying a message clear when that is the task.
  • A method groups instructions around one understandable responsibility.
  • Parameters let a method handle different values without copying its instructions.
  • A return value makes a computed result available to the caller.
  • A module groups related program definitions in a separate unit.

2. Specify the method before writing it

A small design specification makes assumptions visible. State the preconditions: what must be true before the method is called. State its inputs and expected output, then outline the steps connecting them. Preconditions should be relevant to the task; they do not replace a description of what the method does.
For example, a method that adds two scores should receive both scores as parameters rather than depend on particular fixed values. If the method is intended for different scores, hard-coding one example value defeats that purpose. A name that explains the task also helps callers understand how to use the method.
Keep the responsibility focused. A method that calculates a result, displays it, and changes unrelated program data is harder to reuse because a caller cannot easily choose just the calculation. Similarly, group related definitions in a module because they serve a shared purpose, not simply because they are convenient to place together.
  • Specify preconditions, inputs, steps, and expected output.
  • Use parameters for values that should vary across calls.
  • Make the method name and responsibility agree.
  • Group related definitions in a module and keep unrelated responsibilities apart.

3. Trace calls and review the design

To trace a call, match each argument in the call to its parameter, follow the method's instructions using those values, and identify the returned value or action. This check helps reveal whether the design communicates the intended use. A reasoning trace is not a claim that code has been executed.
Review a design by asking: Can another part of the program call the method without copying its instructions? Are the needed values passed in? Is the result or action clear? Are the preconditions stated? Does the name describe one responsibility? If callers must edit the method for every new value, reconsider its parameters and responsibility.
Reuse has limits. A method need not handle every imaginable situation. Aim for a clear component that supports the variations the task actually needs. A short description and traces using different valid inputs can help show whether the design is useful beyond its first example.
  • Trace arguments into parameters and the result back to the caller.
  • Check whether the method is useful without editing its instructions for each call.
  • Design for the variations the task needs.
  • A clear name, focused responsibility, and stated assumptions support reuse.

Worked example

Calculate a total

Design a reusable method for calculating the total cost of a chosen number of identical items. Preconditions: quantity is a whole number greater than or equal to zero, and unit price is a non-negative number. Inputs: quantity and unit price. Expected output: their product. The method should not assume a particular item or price.
Total calculation
argumentsresultQuantity and unit priceCalculate productReturned total

The caller supplies two inputs; the method returns their product.

Python 3

def calculate_total(quantity, unit_price):
    return quantity * unit_price

# Illustrative call:
# calculate_total(4, 2.50) returns 10.0
  1. Choose the responsibility
    The method calculates a total and returns it. Keeping display decisions outside the calculation lets a caller use the value in different ways.
  2. Trace a call
    For the illustrative call with quantity 4 and unit price 2.50, the parameters receive those values. Their product is 10.0, the expected returned value. This is a reasoning trace, not a claim that the code was run.
Answer: The method can be called with different quantities and prices while keeping the calculation in one place.
Check: A caller that needs to display the total can display the returned value; another caller can use it in a larger calculation.

Worked example

Create a status message

Design a method that creates a short status message from a person's name and completed task count. Preconditions: the name is non-empty text and the count is a whole number greater than or equal to zero. Inputs: name and count. Expected output: a message that includes both values. The method should create the text but leave the decision to display it to its caller.

Python 3

def make_status_message(name, completed_count):
    return name + " completed " + str(completed_count) + " tasks."

# Illustrative call:
# make_status_message("Mina", 3)
# returns "Mina completed 3 tasks."
  1. Separate creation from display
    Returning text gives the caller the choice to display it, store it, or use it elsewhere. This focused responsibility makes the method useful in more than one context.
  2. Trace the inputs
    For the illustrative call with name "Mina" and count 3, the returned message is "Mina completed 3 tasks." A different valid name and count produce a corresponding message through the same instructions. These are expected results from reading the method, not reported execution results.
Answer: The method receives changing values as parameters and returns the message without printing it.
Check: If the method contained a fixed name, other callers could not obtain a message for their own name without changing the method.

Worked example

Share a method through a module

A class project needs to format a person's name consistently in two program parts. Design a module called name_tools containing a method that produces a display name from first and last names. Preconditions: both inputs are non-empty text. Inputs: first name and last name. Expected output: the two names joined with one space. Both callers should use the shared method instead of keeping separate copies.
Shared name-formatting module
callscallscontainsProgram part AProgram part Bname_tools modulemake_display_name

Two callers use the same method contained in name_tools.

Python 3

# Contents of name_tools.py
def make_display_name(first_name, last_name):
    return first_name + " " + last_name

# Illustrative use by either caller:
# name_tools.make_display_name("Ari", "Chen")
# returns "Ari Chen"
  1. Decide what belongs together
    Name formatting belongs in a module intended for name-related tools. The callers need that shared service, not separate copies of the formatting instructions.
  2. Trace a shared call
    A call with "Ari" and "Chen" supplies those strings to the two parameters and returns "Ari Chen". A second caller can supply different names and use the same method.
Answer: One module method provides consistent name formatting for both callers and can be changed in one place if the formatting rule changes.
Check: The method joins names, while callers decide what to do with the returned text.

Common mistakes and how to avoid them

Copying the same instructions into several places and updating only some copies.
Correction: Put the shared task in one method and call it wherever needed.
Hard-coding a value that should change from one call to another.
Correction: Make changing information a parameter and supply it as an argument at each call.
Giving a method several unrelated responsibilities.
Correction: Give it one clear purpose and let callers combine focused methods when needed.
Making a method display a result when callers need to use it in different ways.
Correction: Return the result when the caller needs to decide how to use it.
Leaving important assumptions unstated.
Correction: State preconditions so callers know which inputs the method is designed to handle.

Lesson summary

  • Reusable methods collect a focused task behind a clear name.
  • Parameters represent values that vary; return values provide results to callers.
  • A module groups related reusable definitions in one shared place.
  • State preconditions, inputs, steps, and expected output, then check the design with a trace.

Check your understanding

Question 1

A method must calculate a score total for different players. Which design best supports reuse?
  1. Store one player's score inside the method and use it for every call.
  2. Accept the scores as parameters and return the calculated total.
  3. Copy the calculation into each part of the program that needs it.
  4. correctIndex
Show answer and explanation
Accept the scores as parameters and return the calculated total.
Parameters let each caller supply the relevant scores, and returning the total makes the result available to the caller.

Question 2

A method returns a formatted message. Which statement best describes the caller's role?
  1. The caller must edit the method's instructions for each new name.
  2. The caller supplies the method's inputs and can decide what to do with the returned text.
  3. The method can only be used by the first part of the program that calls it.
  4. correctIndex
Show answer and explanation
The caller supplies the method's inputs and can decide what to do with the returned text.
The method handles message creation; the caller supplies values and chooses how to use the returned text.

Question 3

Two program parts need the same name-formatting rule. What is the clearest reusable design?
  1. Maintain a separate copy of the rule in each program part.
  2. Put the related formatting method in one module and have both parts call it.
  3. Have each program part use a different fixed name.
  4. correctIndex
Show answer and explanation
Put the related formatting method in one module and have both parts call it.
One shared module method avoids duplicated instructions and gives both callers the same formatting service.

Key terms

Method
A named group of instructions designed to perform a task and called when that task is needed.
Module
A separate, named unit of program code that can hold related definitions for reuse.
Parameter
A named input in a method definition that receives a value for a particular call.
Argument
A value supplied to a method when it is called.
Precondition
A condition expected to be true before a method is called.
Return value
A result produced by a method and made available to the caller.

Continue through ICS4U

View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons

Tutor support for C1.4 · Design reusable modules and methods

Working on C1.4 · Design reusable modules and methods in ICS4U? Bring the question and your own attempt. In a live session, you can trace a small example and find the first step where the output differs from what you expected. Ask about the first step that does not make sense, then try a similar question yourself.

Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.

برای مبحث C1.4 · طراحی پیمانه‌ها و روش‌های قابل استفاده دوباره در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه می‌توانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامه‌نویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقه‌ای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی می‌شود.

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation C1.4. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question