Day 13 - Function Design and Documentation

Day 13: Function Design and Documentation

Learning Objectives

Essential Questions

Materials Needed

Vocabulary

Procedure (50 minutes)

Opening (8 minutes)

  1. Review and Connection (3 minutes)

    • Review parameters and return values from previous lesson
    • Connect to today's focus on designing and documenting functions
  2. Warm-up Activity (5 minutes)

    • Present an undocumented function with a cryptic name and parameters
    • Ask students to figure out what it does and how to use it
    • Discuss the challenges of using poorly documented functions

Main Activities (32 minutes)

  1. Lecture: Function Design Principles and Documentation (12 minutes)

    • Explain principles of good function design:
      • Single responsibility (each function should do one thing well)
      • Appropriate level of abstraction
      • Clear interface with meaningful parameter names
      • Minimal side effects
      • Reasonable function size
    • Discuss the importance of function documentation:
      • Helps others understand how to use the function
      • Clarifies the function's purpose and behavior
      • Specifies preconditions and postconditions
      • Explains parameters and return values
      • Documents any side effects or exceptions
    • Introduce documentation formats:
      • Header comments
      • Parameter descriptions
      • Return value descriptions
      • Example usage
      • Inline comments for complex logic
    • Explain how good documentation supports abstraction
  2. Demo: Writing Well-Documented Functions (8 minutes)

    • Walk through examples of well-documented functions
    • Show before-and-after examples of poorly vs. well-documented code
    • Demonstrate how to write clear function headers and comments
    • Show how documentation helps users understand the function interface
    • Illustrate how documentation can make complex functions accessible
    • Demonstrate how to document parameters, return values, and preconditions
  3. Hands-on: Documenting Existing Functions (12 minutes)

    • Provide students with undocumented functions
    • Students add appropriate documentation:
      • Function purpose
      • Parameter descriptions
      • Return value descriptions
      • Example usage
      • Any preconditions or assumptions
    • Students may also improve function names and parameter names
    • Encourage students to think about the function from a user's perspective

Closing (10 minutes)

  1. Activity: Peer Review of Function Documentation (5 minutes)

    • Students exchange their documented functions with a partner
    • Partners review the documentation and provide feedback:
      • Is the purpose clear?
      • Are parameters well-described?
      • Is the return value explained?
      • Could they use the function based on the documentation alone?
    • Students revise their documentation based on feedback
  2. Exit Ticket and Preview (5 minutes)

    • Students create and properly document a function for a specific purpose
    • Preview that next class will focus on libraries and APIs

Assessment

Differentiation

For Advanced Students

For Struggling Students

Homework/Extension

Teacher Notes