
3.5. Paint Pot Refactoring and Documentation¶
This lesson modifies the app enhanced in the 'Paint Pot Projects', not to change any functionality, but to improve the code, a process called refactoring. In this case we will introduce the concept of a programmer-defined procedure that will help reduce the complexity of our code and make it easier to read and maintain. A procedure is a named grouping of instructions that can be used where needed in a program by invoking its name. This is known as 'calling a procedure.' This lesson reinforces the enduring understanding that procedures are useful abstractions that are used by programmers to structure and simplify programs (procedural abstraction). We will also add comments as a way of documenting the purpose of the procedure.
CSP Framework | |||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||||||||||||||||||||||||||||||||
Enduring Understanding CRD-2: Developers create and innovate using an iterative design process that is user-focused, that incorporates implementation/feedback cycles, and that leaves ample room for experimentation and risk-taking. | |||||||||||||||||||||||||||||||||||||||||
Learning Objective CRD-2.G: Describe the purpose of a code segment or program by writing documentation. | |||||||||||||||||||||||||||||||||||||||||
Learning Objective CRD-2.H: Acknowledge code segments used from other sources. | |||||||||||||||||||||||||||||||||||||||||
Learning Objective CRD-2.I.a: For errors in an algorithm or program: a. Identify the error. Learning Objective CRD-2.I.b: For errors in an algorithm or program: b. Correct the error. | |||||||||||||||||||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||||||||||||||||||
Enduring Understanding AAP-3: Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing them to write programs more quickly and with more confidence. | |||||||||||||||||||||||||||||||||||||||||
Learning Objective AAP-3.B: Explain how the use of procedural abstraction manages complexity in a program. | |||||||||||||||||||||||||||||||||||||||||
Learning Objective AAP-3.C: Develop procedural abstractions to manage complexity in a program by writing procedures. | |||||||||||||||||||||||||||||||||||||||||
Professional Development
The Student Lesson: Complete the activities for Mobile CSP Unit 3 Lesson 3.5: Paint Pot Refactoring and Documentation.
Materials
- Presentation system (LCD projector/Interactive whiteboard)
- Access to computer, laptop, or Chromebook (install the Companion app on Chromebooks)
- Access to mobile device with the Companion app installed or access to the emulator installed on the computer or laptop.
- Paint Pot Refactoring Text Version or Videos
- Wikipedia article on refactoring
3.5.1. Learning Activities¶
Estimated Length: 45 minutes
- Hook/Motivation (5 Minutes): Here are some suggestions.
- Show the students the lyrics of a children's song where there is a chorus like This Old Man and have them point out any lines that are repeated. These can be pulled out into a procedure called chorus and called whenever needed. This is procedural abstraction.
- Show the students the code from a finished Paint Pot app or the template for this app. Ask them to find the duplicated code, i.e. the same blocks of code that are used in more than one event. They should identify the blocks to update the dot size label. Explain that you will learn how to create procedures, which give a name to a block of code that can then be reused in the program. You will also be adding comments to the procedure to document what it does for yourself and other programmers.
- Experiences and Explorations (20 minutes): Instructors can lead students through the tutorial, can show the video (4:32), or can allow students to watch it on their own. Students can work in pairs to either update their existing app from the Paint Pot Projects lesson, or they can use the template provided in the lesson.
- Rethink, Reflect and/or Revise (20 minutes):
- Students should work on completing the self-check exercises. If time, review them as a class to ensure students are writing procedures correctly.
- In their portfolios, have the students answer the portfolio reflection questions found in the Mobile CSP lesson. Now that they've refactored their code, ask them how many blocks would need to change in the code if they wanted to use a slider or another UI component to display the dot size to the user? Do they think it is easier or harder with the procedure? Can students think of where they might add comments or procedures in other apps they have created?
3.5.2. Professional Development Reflection¶
Discuss the following questions with other teachers in your professional development program.
- How does this lesson help students toward the enduring understanding that programming is facilitated by appropriate abstractions, such as procedures?
-
I am confident I can teach this lesson to my students.
- 1. Strongly Agree
- 2. Agree
- 3. Neutral
- 4. Disagree
- 5. Strongly Disagree
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.