Parameters, Return, and Libraries - Lesson 7: Libraries Practice

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.

Goals

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

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 write code that is "higher level" than previous code you have written. 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.

Resources

Getting Started (5 minutes)

Quick Warm Up

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.

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.

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 minutes)

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 output. 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.

Apps 1-2 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.

  • Lesson7_App1: Students build a simple app with the states library from the investigate lesson
  • Lesson7_App2: Students build a simple app with a new library that uses the cats data set.

Apps 3-5 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.

  • Lesson7_App3: Students debug a simple library for manipulating numbers
  • Lesson7_App4: Students debug a simple library for manipulating strings
  • Lesson7_App5: Students debug and finish writing a simple library for manipulating strings

Extension Opportunities

  • App 1 and 2: Students can add additional features to the two apps, Lesson7_App1 and Lesson7_App2 using the library functions made available.

Wrap Up (5 minutes)

Synthesis

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.

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.

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

For Students

Open a word doc or google doc and copy/paste the following question.

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

  • CSTA K-12 Computer Science Standards (2017): 2-AP-14, 2-AP-17, 3A-AP-18, 3B-AP-16

Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 7 Lesson 8, which describes Learn about parameters and return.