(Also available in WeScheme)
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:
|
Student-facing Goals |
|
Materials |
|
Supplemental Materials |
|
Preparation |
|
Key Points For The Facilitator |
|
🔗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.
Note that in Pyret definitions work the way variable substitution does in math, as opposed to variable assignment you may have seen in other programming languages.
Launch
Invite students to take a look at the expressions below:
star(50, "solid", "green")
scale(3, star(50, "solid", "green"))
scale(0.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:
x = 4 defines the nickname x to be the value 4.
Pyret is no different!
We type 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, x + 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
Open the Defining Values Starter File.
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 Pyret 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.)
-
The name x is unbound:
It is used but not previously defined.
-
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 Pyret to load and read all of the definitions.
If Pyret 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.
-
Open a new file in code.pyret.org (CPO) and name it
sunny
. -
Then turn to Why Define Values? and take a look at the first row of the table.
-
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!
-
🔗Additional Exercises
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). 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.