Lesson 7: Libraries Practice
App Lab
Overview
Students practice important skills for working with libraries, including testing and debugging libraries, and using libraries to help develop apps. After a brief introduction to these practices by the teacher, students spend the majority of their time programming in a level progression.
Purpose
Using libraries highlights a number of important concepts, skills, and mindsets. Here is a short list
- Testing: Before sharing a library it is critical to make sure your functions actually work as intended. The best way to do this is to write tests that try out different cases of your program. By doing this like testing positive and negative numbers, or testing out edge cases, it's possible to assure yourself that your code works as you expect.
- Debugging: Closely related to the point above, debugging libraries can be tricky once the program is shared with another student. While students can test functions that were shared with them, it will be up to the original author of the code to make the changes and ensure their library is bug free.
- Abstraction: Libraries allow you to make blocks that are "higher level" than other blocks in the toolbox. The result is that the actual programs you write either will be shorter, since library code does the heavy lifting, or more complex, because library code allows you to focus on bigger problems. In either case, libraries help you focus on the high level problem you're solving rather than focus on the low level details, and this is an example of abstraction in programming.
Agenda
Lesson Modifications
Warm Up (5 mins)
Activity (35 mins)
Wrap Up (5 mins)
View on Code Studio
Objectives
Students will be able to:
- Test functions designed to be used in a library using different input values
- Debug library code to remove any errors
- Read library code documentation in order to select the proper functions in the library to develop an app
Preparation
- Review the programming levels students will complete
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)
Quick Warm Up
Discussion Goal
Goal: You'll return to this prompt later in the lesson so use this discussion as much to plant the ideas here in students heads as to really hit them home. The main goal you're aiming for is the first bullet in the remarks at the end of the warm up. Libraries take care of detailed tasks so you can focus on the big picture. Students likely will not mention the fact that this increases the need to debug and test code in the first place but this is another idea that will be explored in the lesson.
Prompt: How does using a library allow you to think about programming at "a higher level"?
Discuss: Have students share responses with a neighbor before discussing with the whole class.
Remarks
When we program with libraries we can use functions that take care of difficult or repetitive tasks. Instead of focusing on these details we can focus on the big picture of what it is that we want our programs to do and just assume that those details are handled by library functions. As we'll see in today's lesson that leads to two important realizations.
- Our programs may often be shorter and easier to read
- We need to debug libraries carefully before we share them to make sure they really work
Activity (35 mins)
Practice Time
Group: It is recommended that students work in pairs for this lesson.
Remarks
Today you're mostly going to practice what we've learned about programming with libraries. In general we want to use the debugging process we've practiced all year, but here's some specific things to look out for in today's lesson.
- Test library code using console.log. Call the function with many different values to make sure it works as you expect
- It's much harder for someone to debug library code once it's sent to them. You'll need to make sure it works on your end.
Teaching Tip
Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.
Levels 2-3 Using Libraries: These levels involve students using libraries. These levels focus on students understanding that libraries make the programs that they write simpler and higher level.
- Level 2: Students build a simple app with the states library from the investigate lesson
- Level 3: Students build a simple app with a new library that uses the cats data set.
Levels 4-6 Debugging Libraries: Students practice debugging code that will be used in a library before sending it to a classmate. This is useful help before the project in the next lesson where students will develop a library.
- Level 4: Students debug a simple library for manipulating numbers
- Level 5: Students debug a simple library for manipulating strings
- Level 6: Students debug and finish writing a simple library for manipulating strings
Extension Opportunities:
- Levels 2 and 3: Students can add additional features to the two apps in levels 2 and 3 using the library functions made available.
Wrap Up (5 mins)
Discussion Goal
Goal: These prompts review the main things students should have seen in today's lesson and that were previewed in the Warm Up. Students should be able to better speak from experience about the fact that the programs they wrote were significantly simpler because they could just use library code. They should also have seen how testing of libraries is important because the user of their library is counting on the functions working exactly as they say they're supposed to.
Prompt: How do libraries let you write programs at a "higher level"? Why is testing important when building and sharing libraries?
Discuss: Have students share with one another before sharing with the whole class.
Remarks
Libraries are yet another level of procedural abstraction. They allow us to write programs that are high level and focus on the big picture. Our programs are shorter, and they read more like exactly what they are doing. This doesn't mean that there isn't more detailed code being run, it just means that once the library is written, perhaps by someone else, we don't have to think about it as much anymore.
We're about to do a class project that will test our ability to think in this new way.
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: The following procedure was developed to determine if a list contains a negative number. If the list contains a negative number it should return true, otherwise it should return false.
An error was made in writing this function so that it does not work as intended. Which line of code would need to be fixed in order for the function to work as designed?
01 PROCEDURE checkNegative(list) 02 { 03 hasNegative <- true 04 FOR EACH number IN list 05 { 06 IF(number < 0) 07 { 08 hasNegative <- true 09 } 10 } 11 RETURN(hasNegative) 12 }
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.