Lesson 6: Libraries Investigate
App Lab
Overview
In this lesson students work with partners to investigate two apps that use libraries as well as the code used to make a library. Through a series of guided discussions they learn how libraries help programmers simplify and reuse their code.
Purpose
Students get a chance to see how libraries are used in actual apps before they begin building libraries themselves in the subsequent project. The sample apps are written in a clean and simple manner, helping students understand the usefulness of well-documented libraries.
Agenda
Lesson Modifications
Warm Up (5 mins)
Activity (35 mins)
Wrap Up (5 mins)
View on Code Studio
Objectives
Students will be able to:
- Identify the use of a library within a program.
- Explain the purpose of libraries as a way to simplify programs, allow for code reuse, and enable collaboration.
- Test the functions in libraries in order to understand their behavior
Preparation
- Read the code for both sample apps and their associated libraries
- Practice sharing and importing libraries yourself in order to understand the process
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the Teachers
- CSP Unit 7 - Parameters, Return, and Libraries - Presentation
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)
Prompt: Today we are going to learn how to use libraries to share code with one another. Usually you do this by writing functions with parameters and return values. Why do you think it's important to use parameters and return values when writing code for other people to use?
Discussion Goal
Some points that might come up:
- Parameters and return values allow functions to run in predictable ways without impacting other parts of the app unexpectedly.
- Parameters and return values help communicate what the code is supposed to do.
- Parameters and return values make functions more flexible so that they can be used in a variety of situations.
Activity (35 mins)
Group: Place students in pairs. One student per group should navigate to the lesson on Code Studio.
Level 2
Do This: Students navigate to Level 2 where they should run the app and try several different inputs.
Discussion Goal
When the button is clicked, the screen is updated by calling several functions from a library which return text and images that is displayed.
Discuss: With their partners, students look at the project code and discuss what happens when the button is clicked. After a few minutes, pull the class back together and discuss as a whole.
Do This: Now students should open the functions drawer and look specifically at each StateLibrary function. They should mouseover the functions for the documentation before discussing with a partner how these functions work.
Do This: Then students click "Manage Libraries" and "view code" following the screenshots on the slide. Students read the library code and discuss how the functions actually work. They should consider if they were accurate in their predictions before looking at the library code.
Discussion Goal
Benefits: The project code is neat and organized.
Information: The end user does not have to know all the inner workings of the functions - they just need to know the documentation for the functions so they can call them correctly and understand what will be returned (if anything).
Discuss: What are the benefits of hiding all of the code for filtering the dataset in a library? What information does the user need to know in order to use the library functions?
Level 3
Do This: Students navigate to Level 3 where they run the Pigify app and try several inputs.
Do This: Again, students open the functions drawer, look at the documentation for each function, and discuss how they work.
Test the Functions: Now students practice testing the functions to understand how they work. Here are the steps:
- Re-read the documentation for each library function
- Add a
console.log()
statement to the end of the program and call a function. Put in a reasonable argument in the space for the parameter.- For example:
console.log(StringsLibrary.firstLetter("pizza"));
- For example:
- Hit run to see the output.
- Now add
console.log()
statements to test the rest of the functions. Is the output what you would expect? Try several different inputs.
Discussion Goal
Goal: Testing the functions allows the end user to understand the behavior of the function. It's helpful for debugging functions, in addition to looking at the code in the library.
For example: If I call a function whose documentation indicates that it will return the first letter of a string, and instead it returns the last number of a string, I know that there is a problem with the library function and not my project code.
Prompt: Why should we test the functions in the library? What does this help us to know?
console.log
to test all of the library functions there.
Discussion Goal
Goal: Hopefully through the process of testing, students will understand the importance of well-documented functions that do what they are expected to do!
A good library function contains everything it needs within the function. Beware of global variables or references to element IDs that might not be present in the end user's projects
Prompt: What makes a good library function? How can you make sure that the end users of your library have what they need in order to use your functions?
Review: Up to this point, students have either created their own algorithms from scratch, or modified existing ones (usually in Investigate Lessons). Now they have another tool to use: combining existing algorithms to make new algorithms. Here, this is accomplished with a library.
Discussion Goal
Goal: The benefits of using existing algorithms as building blocks for constructing other algorithms include:
- reduced development time
- reduced testing
- simplification and identification of errors
We demonstrated the last two bullets in the exercise using console.log to test functions and isolate errors.
Prompt: What are the benefits of using existing algorithms instead of brand new algorithms?
Examples of existing algorithms you may have seen:
- the maximum or minimum of 2 or more numbers
- the sum or average of 2 or more numbers
- an algorithm that determines if an integer can be evenly divided by another integer
- a robot's path through a maze
Remarks
Now let's review Procedural Abstraction. In Lesson 2, we learned about Procedural Abstraction, where shared features of functions (also known as procedures) are extracted in order to generalize use. Procedural Abstraction encourages code reuse and manages complexity, because generalized functions can be used to accomplish many different tasks.
Now let's extend that definition. Procedural Abstraction provides a name for a process and allows the procedure to be used only knowing what it does, and not necessarily how it does it.
This is how our libraries work!
There's a term for using libraries or other forms of organization in a program: Modularity - the subdivision of a computer program into separate subprograms. This is what we are doing when we organize functions into a library and then call them in another program.
The end user of the library only needs to know how the functions work, which they can learn through the documentation. This means that the creator of the library can update the functions for any number of reasons, such as making the functions more efficient, without having to notify the end users, as long as the function documentation does not need to be changed. The end users can expect the library to work as intended.
Do This: Review the takeaways on the slide. These are focused on the practicalities of creating and using libraries.
Wrap Up (5 mins)
Synthesis
Prompt: Based on what you saw today, add reasons why someone would argue for the following three statements
- Libraries help programmers collaborate
- Libraries help programmers reuse code
- Libraries help programmers writer simpler programs
Discussion Goal
As you discuss focus conversation on the following points.
- Libraries help programmer collaborate because they can design code with the idea that other people will use them.
- Libraries help programmers reuse code because they can be imported into many different programs
- Libraries help programmers write simpler programs because library code can be used to handle many common (and sometimes complex) behavior and remove it from your core program.
Discuss: Have students discuss together before sharing with a class.
Journal: Students add to their journal the following word and definition: modularity. They may also want to update their definition for procedural abstraction based on earlier conversations and slides.
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: Why is it important to use meaningful names for the functions in your library?
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.
- 2-AP-17 - Systematically test and refine programs using a range of test cases.
- 3A-AP-18 - Create artifacts by using procedures within a program, combinations of data and procedures, or independent but interrelated programs.
- 3B-AP-16 - Demonstrate code reuse by creating programming solutions using libraries and APIs.
- 3B-AP-23 - Evaluate key qualities of a program through a process such as a code review.
CSP2021
AAP-2 - The way statements are sequenced and combined in a program determines the computed result
AAP-2.M - For algorithms: a. Create algorithms. b. Combine and modify existing algorithms.
- AAP-2.M.1 - Algorithms can be created from an idea, by combining existing algorithms, or by modifying existing algorithms.
- AAP-2.M.2 - Knowledge of existing algorithms can help in constructing new ones. Some existing algorithms include:● determining the maximum or minimum value of 2 or more numbers ● computing the sum or average of 2 or more numbers● identifying if a
- AAP-2.M.3 - Using existing correct algorithms as building blocks for constructing another algorithm has benefits such as reducing development time, reducing testing, and simplifying the identification of errors.
AAP-3 - Programmers break down problems into smaller and more manageable pieces
AAP-3.B - For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.
- AAP-3.B.1 - One common type of abstraction is procedural abstraction, which provides a name for a process and allows a procedure to be used only knowing what it does, not how it does it.
- AAP-3.B.2 - Procedural abstraction allows a solution to a large problem to be based on the solution of smaller subproblems. This is accomplished by creating procedures to solve each of the subproblems.
- AAP-3.B.3 - The subdivision of a computer program into separate subprograms is called modularity.
- AAP-3.B.4 - A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program code reuse, which helps manage complexity.
- AAP-3.B.5 - Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
- AAP-3.B.6 - Using procedural abstraction helps improve code readability.
- AAP-3.B.7 - Using procedural abstraction in a program allows programmers to change the internals of the procedure (to make it faster, more efficient, use less storage, etc.) without needing to notify users of the change as long as what the procedure does is preserved
AAP-3.D - Select appropriate libraries or existing code segments to use 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.