instagram

(Also available in Pyret)

Students learn to improve readability, performance and maintainability of code by defining values that repeat in code, just as we would define variables in math.

Lesson Goals

Students will be able to:

  • Define values of various types

  • Simplify a complex expression by replacing repeated parts with defined names,

  • Explain why variables are useful in math and programming

Student-facing Goals

  • Let’s learn how to clean up complex code by defining values for repeated expressions

  • Let’s learn why variables are useful in math and programming

Materials

Supplemental Materials

Preparation

  • There is an optional interactive Desmos activity in the lesson. If you would like to use it, decide how you will share it with students and, if you are using our Google Slides, add the appropriate link to the slide deck. If you’re a first-time Desmos user, fear not! Here’s what you need to do.

Key Points For The Facilitator

  • Learning how to define values is a big milestone! It will be used consistently throughout other lessons, so be sure to give students plenty of time to practice this new skill.

  • Check frequently for understanding of data types and Contracts during this lesson and throughout subsequent lessons.

🔗What’s in Common? 30 minutes

Overview

This activity introduces the problem with duplicate code, leveraging Mathematical Practice 7 - Identify and Make Use of Structure. Students identify a common structure in a series of expressions, and discover how to bind that expression to a name that can be reused.

Launch

Invite students to take a look at the expressions below:

(star 50 "solid" "green")
(scale 3 (star 50 "solid" "green"))
(scale .5 (star 50 "solid" "green"))
(rotate 45 (star 50 "solid" "green"))
(rotate 45 (scale 3 (star 50 "solid" "green")))
  • What code do they all have in common?

    • (star 50 "solid" "green")

  • What would happen if you were asked to change the color of all the stars to gold?

    • We’d have to change the color everywhere it appeared.

There are lots of potential problems with duplicate code:

  • Readability: The more code there is, the harder it can be to read.

  • Performance: Why re-evaluate the same code a dozen times, when we can evaluate it once and use the result as many times as we need?

  • Maintainability: Suppose we needed to change the size of the stars in the examples above. We would have to make sure every line is changed, which leaves a lot of room for error.

Duplicate code is almost always bad!

Since we’re using that star over and over again, wouldn’t it be nice if we could define a "nickname" for that code, and then use the nickname over and over in place of the expression? And then, if we wanted to change something about all of the stars, we would only have to make the change once, in the definition.

You already know how to do this in math:

𝑥 = 4 defines the nickname 𝑥 to be the value 4.

WeScheme uses the word "define" to make this even clearer!

We can type (define x 4) to define x to be the value 4.

Once we’ve defined x to be the value 4 and clicked "Run", anytime we use x, the computer will remember that it is defined as 4. We can use that definition to get an answer. For example, 𝑥 + 2 will evaluate to 6.

Of course, the whole point of defining a value is so that it sticks around and can be used later! That’s why programmers put their definitions on the left-hand side, known as the Definitions Area.

Investigate

This file has a lot of values defined on the left, including the example we just talked about: x is defined to be 4.

  • What do you expect WeScheme will produce if we type x into the Interactions Area and hit Enter?

    • We expect the number 4 to come back

  • If you got ahead of us and clicked "Run", please reload the starter file now before proceeding.

  • Type 4 into the Interactions Area and hit return/enter. What did you get back?

    • An error! (Assuming students followed your directions and didn’t hit "Run" yet.)

x: this variable is not defined
at: line 1, column 0, in <interactions0>
  • What do you think this error message means?

    • Answers will vary…​ but it’s telling us that the definition is missing, and that means we need to click "Run"!

The "Run" button tells WeScheme to load and read all of the definitions.
If WeScheme hasn’t run the program we just loaded, it doesn’t know about any of the definitions!

  • With your partner, complete Defining Values - Explore.

  • Add some definitions of your own in the Definitions Area.

  • Be sure to click "Run" again before you try testing them out.

Synthesize

  • What data types can we define values for?

    • All of them - Number, String, Image…​

  • In question 13, you looked at different ways of writing the same definition. Each way broke the definition up into multiple lines. Which one did you like best, and why?

  • What new variables did you decide to define? When might they be useful?

Support for English Language Learners

MLR 8 - Discussion Supports: As students discuss, rephrase responses as questions and encourage precision in the words being used to reinforce the meanings behind some of the programming-specific language, such as "define" and "value".

🔗Look for and Make Use of Structure

Overview

Now that we know how to define values, we’ve got two more things to consider:

  • When it would be useful to define them?

  • How do we use them once we’ve defined them?

Launch

Once you know how to define values, you can start looking for re-usable logic and ways to simplify the solution to a problem.

The page which students are about to work with (Which Value(s) Would it Make Sense to Define?) is best discussed with color versions, but they are likely working with black and white versions. We recommend projecting a version of the file at the front of the room for reference.

Turn to Which Value(s) Would it Make Sense to Define? and identify the shapes it would make sense to "reuse" when building these flag images?

Investigate

  • Now that we’ve thought about why it might make sense to define and reuse values in our code, let’s dig into cleaning up some code!

  • Complete Chinese Flag.

This worksheet will direct students to open the Flag of China Starter File once they complete the first half of the questions.

Have students share their answers about why the code for the Chinese Flag is broken up into multiple lines, and what they think it means for two or more lines to start at the same position.

In this exercise, you saw a really long definition that is broken up into many lines. In this example, all of the inputs to each put-image followed a pattern:

  • The first line contains the image being put on top

  • The second line contains the x- and y-coordinates that position that image on the bottom image

  • The last line contains the code for the bottom image.

This makes the code a lot easier to read! Programmers break up their code in sensible ways in order to communicate the structure of the program.

  • What is happening in that first row?

    • The original Circle of Evaluation has been simplified by using a defined value sunny.

  • Find the code that is being replaced by sunny and write it on the line at the top of the page.

    • (radial-star 30 20 50 "solid" "yellow")

  • Complete Why Define Values?.

  • Then add a definition for sunny in the Definitions Area of your file and test your code in the editor.

  • When you’re done, turn to Writing Code using Defined Values and follow the directions to work with a new definition called PRIZE-STAR.

Synthesize

  • Why is defining values useful?

    • Defining values allows the programmer to reuse code and make changes easily. It allows us to more easily use elements inside other functions, and it saves time!

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). CCbadge Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.