Lesson 5: Libraries Explore

Overview

Students learn the basics of libraries while building upon the envelope model of a function with a folder to represent a library.

Purpose

The mental model of a folder containing a group of functions is used to introduce the concept of a library. Throughout this lesson student explore the requirements for working with libraries and documentation.

Agenda

Lesson Modifications

Warm Up (5 mins)

Activity (35 mins)

Wrap Up (10 mins)

View on Code Studio

Objectives

Students will be able to:

  • Use appropriate vocabulary to describe libraries
  • Explain the process of preparing a function to be added to a library
  • Clearly write documentation for functions in a library

Preparation

  • Review the slides and click through all the animations
  • Watch the Libraries video which introduces the Libraries tool in App Lab

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

Teaching Guide

Lesson Modifications

Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.

Warm Up (5 mins)

Preview Libraries

Discussion Goal

Goal: Guide student answers towards possible answers like these -

  • email code to each other
  • used a shared document
  • copy/paste functions from one tab to another

Prompt: How could you share a function with another person so they could use it in their own program?

Remarks

Today we are going to learn a new way to share code.

Activity (35 mins)

Display: Use the activity slides for this lesson to guide the unplugged activity on Libraries.

Slides                     Speaker Notes
Say: Have you ever wanted to share some of your code with a friend so they can use it to add a cool feature in their program? Or maybe you've got a collection of functions in one program that you want to use in another program. Today we are going to explore the answer to this question: How can we easily share functions between programs?
Click for animation: Stop once all three envelopes are on the screen.

Say: These are functions. We recently learned about functions with parameters and return values.

Click for animation: Click until the library definition appears on the screen.

Say: The envelopes have been grouped together in a folder. This represents a library. A library is a collection of functions that can be used in many different programs.
Say: Let's take a look at what's inside the library. If we open up the folder, we see the functions and some documentation. A library should have documentation for each of the included functions. The documentation should include:
  • how each function works
  • a complete list of the parameters
  • what (if anything) will be returned
Say: This detailed type of documentation is also known as Application Program Interface(API). APIs are specifications for how the functions in a library behave and can be used.

Discuss: With a partner, review the parts of the documentation.
Discuss: What potential problems could come up if I tried to use a function without knowing what it does or how to interact with it? This would be similar to looking at the front of a function envelope and having to guess:
  • what the function does
  • what data type the parameters need
  • what is returned
Note: If students are struggling to come up with ideas, give a few hints:
  • what if there's an error when you run the code which calls this function?
  • what happens to a return if you don't store it in a variable or print it to the console?
Discuss: My friend wants to use my findSmallest() function in her program. Is this function ready to be shared in a library?

Click for animation

Say: Watch out for global variables! If a function accesses or updates a variable elsewhere in your program, that function shouldn't be shared as is.

Click for animation

Do This: With a partner, rewrite the function so it could be shared in a library.

Click for animation

Note: The problem here, is that the function uses a global variable. Students should try to rewrite the function so only local variables and a return are used. The answer is shown later on in the slides.
Say: These are the things you should consider before adding a function to a library:
  1. Check for any use of a global variable within the function. If there is, rework the function using local variables and a return.
  2. Check if another function is called in this function. If so, both functions should be included in the library.
  3. Write the documentation for the function.
Say: Here's the same function, now written to be shared in a library. How does it look compared to yours? It may look a little different - and that's ok, as long as the function works as expected. Now my function is almost ready to be shared in a library. With a partner, write the API for this function:
  • how the function works
  • all the parameters, their data types, and a short description of each
  • what (if anything) will be returned
Click for animation When the class is ready, click through to see a possible answer.
Say: A library needs a name. For our libraries in this class, we are going to follow the following rules:
  • No spaces
  • Capitalize the first letter
Click for animation
Say: This library can now be shared with others. They can use the functions within their own programs as long as they follow the rules set forth in the documentation.

Click for animation
Say: You've seen libraries in action before. The Math library is built into App Lab. Notice the name of the library first, then after a dot, the name of the function and finally the parameters. This is the same formatting you will use to call functions in libraries: Library name, followed by function name, and finally the parameter(s)
Do This: Brainstorm with a partner a few functions that might show up in the following libraries: Calculator, DisplayLists, FiterDatabase.

Note: Here are some ideas:
  • Calculator: functions similar to what you would find in a common calculator
  • DisplayLists: functions that help you display a list on a screen in different ways, such as one list item per line or divided with bullet points
  • FilterDatabase: functions that allow you to filter a database for different things, like all the items that start with the letter "a"

Remarks

Now let's take a look at how libraries work in App Lab.

Video: Watch the Libraries video.

Wrap Up (10 mins)

Journal: Have students add the following words to their journals: library, API

Remarks

Great work today! Libraries are different than other programming concepts we've explored like variables and conditions. They offer a way to organize and share your code with others.


Assessment: Check For Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

Question: Evaluate if the following function is a good candidate to be placed in a library. Why or why not?

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-14 - Create procedures with parameters to organize code and make it easier to reuse.
  • 3B-AP-16 - Demonstrate code reuse by creating programming solutions using libraries and APIs.

CSP2021

AAP-3 - Programmers break down problems into smaller and more manageable pieces
AAP-3.D - Select appropriate libraries or existing code segments to use in creating new programs.
  • AAP-3.D.1 - A software library contains procedures that may be used in creating new programs.
  • AAP-3.D.2 - Existing code segments can come from internal or external sources, such as libraries or previously written code.
  • AAP-3.D.3 - The use of libraries simplifies the task of creating complex programs.
  • AAP-3.D.4 - Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
  • AAP-3.D.5 - Documentation for an API/library is necessary in understanding the behavior(s) provided by the API/library and how to use them.