instagram

(Also available in WeScheme)

Students learn to improve readability, performance and maintanability 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

  • I can define names for values, generated by computed expressions

  • I can clean up complex code by defining repeated expressions

  • I can explain why variables are important

Materials

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.

  • Students will use their Contracts page frequently, so it should be kept in an accessible, convenient location.

Glossary
contract

a statement of the name, domain, and range of a function

data type

a way of classifying values, such as: Number, String, Image, Boolean, or any user-defined data structure

value

a specific piece of data, like 5 or "hello"

variable

a name or symbol that stands for some value or expression, often a value or expression that changes

🔗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 re-used.

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.

Duplicate code is almost always bad!

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.

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

Synthesize

  • What data types can we define values for?

    • All of them - Number, String, Image

  • What values 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".

🔗Using Defined Values

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

Note: The above worksheet will direct students to open the Chinese Flag Starter File once they complete the first half of the questions.

Investigate

  • Open a new file in code.pyret.org (CPO) and name it sunny, then turn to Why Define Values?

  • The first row of the table has been completed for you. What is happening in that first row?

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

  • What code is being replaced by sunny?

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

  • Write the code on the line provided in question 2. Then type it into the Interactions Area and click "Run".

  • Define the value sunny in the definitions area.

  • Complete the page and test your code in the editor.

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, and 1738598). 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.