← Teacher guide

Tidy up the journal

Students refactor the values journal: they change how it's built without changing what it does. Four near-identical button events become one function, Pick(value), that each button calls with its own value; then a Home event written on two screens becomes one All screens event. A journey run before and after is the test that nothing changed. They hand in by sharing a link.

App Builder apps · Tidy up · about 50 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Run it 5 min · Spot 5 min · Tidy 1 15 min · Nib 5 min · Tidy 2 12 min · Same journey 5 min · Hand in 3 min

Before class

This lesson starts from the finished values journal, so students who did that lesson will recognise it. Students who missed it can still do this one: the first step has them use the app before changing it.

Refactoring is a real skill in any app or language: spot repetition, pull it into one place, then run the same test again. The point to land is the last part. A refactor that changes what the app does is a bug, however tidy it looks.

Functions and All screens are Level 4. Each step opens App Builder at Level 4, so both show in the sidebar.

Inside Pick, value is a plain name (no quote marks). The call gives it a value: Call Pick with value = "Courage" (quote marks, because there it's text).

An All screens event runs on every screen, matched by object name. On Choose there's no HomeButton, so the event never happens there. Renaming AnotherButton to HomeButton keeps its text ("Add another"): the name is what events use, the text is what the user sees.

Hand-in: Share puts the whole app inside the link (nothing is uploaded), and the link opens a play-only page with "Open a copy in App Builder". Save gives a file instead (a .json, or a .zip when the app has its own pictures or sounds).

Running the lesson

  1. Spot the repetition (whole class, on the deck): put the four events side by side. Circle what's the same, underline what's different.
  2. Tidy 1: checkpoint: does each button still go to Describe with the right question? Fix that before Tidy 2.
  3. Tidy 2: ask what would happen if we added a fourth screen with a HomeButton. (Nothing to add: the All screens event already covers it.)
  4. Same journey: pairs swap and do each other's journey, comparing with the notes from the start.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

The hard part is the parameter. Students often type "value" with quote marks inside Pick, which sets Chosen to the word value. Ask what Chosen shows on the left when they Run; a parameter is a plain name, like a variable.

When students get stuck

What you will seeA question to askThe fix
Clicking a value does nothing.What did the old events do that Pick doesn't?Add Go to screen Describe to Pick, after Set variable Chosen to value.
The question says 'What did you do to show value?'In Pick, is value inside quote marks?Remove the quote marks: Set variable Chosen to value, with value as a plain name.
Home works on Describe but not on Journal.What is the Journal screen's button called now?Rename AnotherButton to HomeButton; the All screens event looks for that name.
The check for Tidy 2 doesn't tick, but Home works.Are the old Home events still on Describe or Journal?Delete them: the All screens event does the job on both screens.

Answers to the checks

  1. Spot the repetition: Open the Choose screen's Events tab. The four value buttons each have an event. What is different between them?
    Only the value they set Chosen to
    All four set Chosen and go to Describe. Only the value changes. The same part becomes the function's actions; the part that changes becomes its parameter.
  2. Find Nib's slip: After Nib's tidy-up, clicking a value does nothing you can see. Click the line where Nib first goes wrong.
    line 2
    Line 2: Pick only sets Chosen. The old events also went to Describe, and that action didn't make it into the function, so the app quietly stays on Choose. A refactor must keep everything the old events did.

Solutions

One possible solution for each task. Other correct programs are fine.

Tidy 1: one function, Pick

{"version":1,"name":"Values journal (tidy)","features":{"level":4},"variables":[{"name":"Chosen","value":""},{"name":"Journal","value":[]}],"screens":[{"id":"choose","name":"Choose","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"j1","x":229,"y":30,"w":342,"h":36,"text":"Values journal","visible":true,"vars":{}}]},{"name":"Prompt","kind":"text","instanceVars":[],"instances":[{"id":"j2","x":229,"y":74,"w":342,"h":40,"text":"Which value did you show today?","visible":true,"vars":{}}]},{"name":"KindnessPicture","kind":"image","instanceVars":[],"instances":[{"id":"v0p","x":263,"y":140,"w":100,"h":100,"text":"Kindness","visible":true,"vars":{},"image":"heart"}]},{"name":"KindnessButton","kind":"button","instanceVars":[],"instances":[{"id":"v0b","x":229,"y":250,"w":165,"h":46,"text":"Kindness","visible":true,"vars":{}}]},{"name":"CouragePicture","kind":"image","instanceVars":[],"instances":[{"id":"v1p","x":440,"y":140,"w":100,"h":100,"text":"Courage","visible":true,"vars":{},"image":"star"}]},{"name":"CourageButton","kind":"button","instanceVars":[],"instances":[{"id":"v1b","x":406,"y":250,"w":165,"h":46,"text":"Courage","visible":true,"vars":{}}]},{"name":"HonestyPicture","kind":"image","instanceVars":[],"instances":[{"id":"v2p","x":263,"y":340,"w":100,"h":100,"text":"Honesty","visible":true,"vars":{},"image":"tick"}]},{"name":"HonestyButton","kind":"button","instanceVars":[],"instances":[{"id":"v2b","x":229,"y":450,"w":165,"h":46,"text":"Honesty","visible":true,"vars":{}}]},{"name":"RespectPicture","kind":"image","instanceVars":[],"instances":[{"id":"v3p","x":440,"y":340,"w":100,"h":100,"text":"Respect","visible":true,"vars":{},"image":"smile"}]},{"name":"RespectButton","kind":"button","instanceVars":[],"instances":[{"id":"v3b","x":406,"y":450,"w":165,"h":46,"text":"Respect","visible":true,"vars":{}}]}],"events":[{"id":"jv0","else":false,"conds":[{"type":"onClicked","target":"KindnessButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Kindness\""}}],"children":[]},{"id":"jv1","else":false,"conds":[{"type":"onClicked","target":"CourageButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Courage\""}}],"children":[]},{"id":"jv2","else":false,"conds":[{"type":"onClicked","target":"HonestyButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Honesty\""}}],"children":[]},{"id":"jv3","else":false,"conds":[{"type":"onClicked","target":"RespectButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Respect\""}}],"children":[]}]},{"id":"describe","name":"Describe","objects":[{"name":"Question","kind":"text","instanceVars":[],"instances":[{"id":"j3","x":229,"y":40,"w":342,"h":64,"text":"","visible":true,"vars":{}}]},{"name":"NoteInput","kind":"input","instanceVars":[],"instances":[{"id":"j4","x":229,"y":120,"w":342,"h":46,"text":"","visible":true,"vars":{}}]},{"name":"AddButton","kind":"button","instanceVars":[],"instances":[{"id":"j5","x":229,"y":184,"w":342,"h":48,"text":"Add to my journal","visible":true,"vars":{}}]}],"events":[{"id":"jd1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Question","value":"\"What did you do to show \" & Chosen & \"?\""}],"children":[]},{"id":"jd2","else":false,"conds":[{"type":"onClicked","target":"AddButton"}],"actions":[{"type":"listAdd","value":"uppercase(Chosen) & \": \" & NoteInput.text","list":"Journal"},{"type":"goToScreen","screen":"journal"}],"children":[]}]},{"id":"journal","name":"Journal","objects":[{"name":"Heading","kind":"text","instanceVars":[],"instances":[{"id":"j6","x":229,"y":30,"w":342,"h":36,"text":"My values in action","visible":true,"vars":{}}]},{"name":"Count","kind":"text","instanceVars":[],"instances":[{"id":"j7","x":229,"y":72,"w":342,"h":30,"text":"","visible":true,"vars":{}}]},{"name":"List","kind":"list","instanceVars":[],"instances":[{"id":"j8","x":229,"y":110,"w":342,"h":360,"text":"","visible":true,"vars":{},"source":"Journal"}]},{"name":"AnotherButton","kind":"button","instanceVars":[],"instances":[{"id":"j9","x":229,"y":490,"w":342,"h":48,"text":"Add another","visible":true,"vars":{}}]}],"events":[{"id":"jj1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Count","value":"\"Entries: \" & count(Journal)"}],"children":[]},{"id":"jj2","else":false,"conds":[{"type":"onClicked","target":"AnotherButton"}],"actions":[{"type":"goToScreen","screen":"choose"}],"children":[]}]}],"device":"web","functions":[{"id":"fpick","name":"Pick","params":["value"],"events":[{"id":"fp1","else":false,"conds":[],"actions":[{"type":"setVar","var":"Chosen","value":"value"},{"type":"goToScreen","screen":"describe"}],"children":[]}]}]}

Tidy 2: one Home event for every screen › Add Home the old way

{"version":1,"name":"Values journal (tidy)","features":{"level":4},"variables":[{"name":"Chosen","value":""},{"name":"Journal","value":[]}],"screens":[{"id":"choose","name":"Choose","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"j1","x":229,"y":30,"w":342,"h":36,"text":"Values journal","visible":true,"vars":{}}]},{"name":"Prompt","kind":"text","instanceVars":[],"instances":[{"id":"j2","x":229,"y":74,"w":342,"h":40,"text":"Which value did you show today?","visible":true,"vars":{}}]},{"name":"KindnessPicture","kind":"image","instanceVars":[],"instances":[{"id":"v0p","x":263,"y":140,"w":100,"h":100,"text":"Kindness","visible":true,"vars":{},"image":"heart"}]},{"name":"KindnessButton","kind":"button","instanceVars":[],"instances":[{"id":"v0b","x":229,"y":250,"w":165,"h":46,"text":"Kindness","visible":true,"vars":{}}]},{"name":"CouragePicture","kind":"image","instanceVars":[],"instances":[{"id":"v1p","x":440,"y":140,"w":100,"h":100,"text":"Courage","visible":true,"vars":{},"image":"star"}]},{"name":"CourageButton","kind":"button","instanceVars":[],"instances":[{"id":"v1b","x":406,"y":250,"w":165,"h":46,"text":"Courage","visible":true,"vars":{}}]},{"name":"HonestyPicture","kind":"image","instanceVars":[],"instances":[{"id":"v2p","x":263,"y":340,"w":100,"h":100,"text":"Honesty","visible":true,"vars":{},"image":"tick"}]},{"name":"HonestyButton","kind":"button","instanceVars":[],"instances":[{"id":"v2b","x":229,"y":450,"w":165,"h":46,"text":"Honesty","visible":true,"vars":{}}]},{"name":"RespectPicture","kind":"image","instanceVars":[],"instances":[{"id":"v3p","x":440,"y":340,"w":100,"h":100,"text":"Respect","visible":true,"vars":{},"image":"smile"}]},{"name":"RespectButton","kind":"button","instanceVars":[],"instances":[{"id":"v3b","x":406,"y":450,"w":165,"h":46,"text":"Respect","visible":true,"vars":{}}]}],"events":[{"id":"jv0","else":false,"conds":[{"type":"onClicked","target":"KindnessButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Kindness\""}}],"children":[]},{"id":"jv1","else":false,"conds":[{"type":"onClicked","target":"CourageButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Courage\""}}],"children":[]},{"id":"jv2","else":false,"conds":[{"type":"onClicked","target":"HonestyButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Honesty\""}}],"children":[]},{"id":"jv3","else":false,"conds":[{"type":"onClicked","target":"RespectButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Respect\""}}],"children":[]}]},{"id":"describe","name":"Describe","objects":[{"name":"Question","kind":"text","instanceVars":[],"instances":[{"id":"j3","x":229,"y":40,"w":342,"h":64,"text":"","visible":true,"vars":{}}]},{"name":"NoteInput","kind":"input","instanceVars":[],"instances":[{"id":"j4","x":229,"y":120,"w":342,"h":46,"text":"","visible":true,"vars":{}}]},{"name":"AddButton","kind":"button","instanceVars":[],"instances":[{"id":"j5","x":229,"y":184,"w":342,"h":48,"text":"Add to my journal","visible":true,"vars":{}}]},{"name":"HomeButton","kind":"button","instanceVars":[],"instances":[{"id":"dh1","x":229,"y":490,"w":342,"h":48,"text":"Home","visible":true,"vars":{}}]}],"events":[{"id":"jd1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Question","value":"\"What did you do to show \" & Chosen & \"?\""}],"children":[]},{"id":"jd2","else":false,"conds":[{"type":"onClicked","target":"AddButton"}],"actions":[{"type":"listAdd","value":"uppercase(Chosen) & \": \" & NoteInput.text","list":"Journal"},{"type":"goToScreen","screen":"journal"}],"children":[]},{"id":"dh2","else":false,"conds":[{"type":"onClicked","target":"HomeButton"}],"actions":[{"type":"goToScreen","screen":"choose"}],"children":[]}]},{"id":"journal","name":"Journal","objects":[{"name":"Heading","kind":"text","instanceVars":[],"instances":[{"id":"j6","x":229,"y":30,"w":342,"h":36,"text":"My values in action","visible":true,"vars":{}}]},{"name":"Count","kind":"text","instanceVars":[],"instances":[{"id":"j7","x":229,"y":72,"w":342,"h":30,"text":"","visible":true,"vars":{}}]},{"name":"List","kind":"list","instanceVars":[],"instances":[{"id":"j8","x":229,"y":110,"w":342,"h":360,"text":"","visible":true,"vars":{},"source":"Journal"}]},{"name":"AnotherButton","kind":"button","instanceVars":[],"instances":[{"id":"j9","x":229,"y":490,"w":342,"h":48,"text":"Add another","visible":true,"vars":{}}]}],"events":[{"id":"jj1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Count","value":"\"Entries: \" & count(Journal)"}],"children":[]},{"id":"jj2","else":false,"conds":[{"type":"onClicked","target":"AnotherButton"}],"actions":[{"type":"goToScreen","screen":"choose"}],"children":[]}]}],"device":"web","functions":[{"id":"fpick","name":"Pick","params":["value"],"events":[{"id":"fp1","else":false,"conds":[],"actions":[{"type":"setVar","var":"Chosen","value":"value"},{"type":"goToScreen","screen":"describe"}],"children":[]}]}]}

Tidy 2: one Home event for every screen › Tidy them into one

{"version":1,"name":"Values journal (tidy)","features":{"level":4},"variables":[{"name":"Chosen","value":""},{"name":"Journal","value":[]}],"screens":[{"id":"choose","name":"Choose","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"j1","x":229,"y":30,"w":342,"h":36,"text":"Values journal","visible":true,"vars":{}}]},{"name":"Prompt","kind":"text","instanceVars":[],"instances":[{"id":"j2","x":229,"y":74,"w":342,"h":40,"text":"Which value did you show today?","visible":true,"vars":{}}]},{"name":"KindnessPicture","kind":"image","instanceVars":[],"instances":[{"id":"v0p","x":263,"y":140,"w":100,"h":100,"text":"Kindness","visible":true,"vars":{},"image":"heart"}]},{"name":"KindnessButton","kind":"button","instanceVars":[],"instances":[{"id":"v0b","x":229,"y":250,"w":165,"h":46,"text":"Kindness","visible":true,"vars":{}}]},{"name":"CouragePicture","kind":"image","instanceVars":[],"instances":[{"id":"v1p","x":440,"y":140,"w":100,"h":100,"text":"Courage","visible":true,"vars":{},"image":"star"}]},{"name":"CourageButton","kind":"button","instanceVars":[],"instances":[{"id":"v1b","x":406,"y":250,"w":165,"h":46,"text":"Courage","visible":true,"vars":{}}]},{"name":"HonestyPicture","kind":"image","instanceVars":[],"instances":[{"id":"v2p","x":263,"y":340,"w":100,"h":100,"text":"Honesty","visible":true,"vars":{},"image":"tick"}]},{"name":"HonestyButton","kind":"button","instanceVars":[],"instances":[{"id":"v2b","x":229,"y":450,"w":165,"h":46,"text":"Honesty","visible":true,"vars":{}}]},{"name":"RespectPicture","kind":"image","instanceVars":[],"instances":[{"id":"v3p","x":440,"y":340,"w":100,"h":100,"text":"Respect","visible":true,"vars":{},"image":"smile"}]},{"name":"RespectButton","kind":"button","instanceVars":[],"instances":[{"id":"v3b","x":406,"y":450,"w":165,"h":46,"text":"Respect","visible":true,"vars":{}}]}],"events":[{"id":"jv0","else":false,"conds":[{"type":"onClicked","target":"KindnessButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Kindness\""}}],"children":[]},{"id":"jv1","else":false,"conds":[{"type":"onClicked","target":"CourageButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Courage\""}}],"children":[]},{"id":"jv2","else":false,"conds":[{"type":"onClicked","target":"HonestyButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Honesty\""}}],"children":[]},{"id":"jv3","else":false,"conds":[{"type":"onClicked","target":"RespectButton"}],"actions":[{"type":"callFunction","fn":"Pick","args":{"value":"\"Respect\""}}],"children":[]}]},{"id":"describe","name":"Describe","objects":[{"name":"Question","kind":"text","instanceVars":[],"instances":[{"id":"j3","x":229,"y":40,"w":342,"h":64,"text":"","visible":true,"vars":{}}]},{"name":"NoteInput","kind":"input","instanceVars":[],"instances":[{"id":"j4","x":229,"y":120,"w":342,"h":46,"text":"","visible":true,"vars":{}}]},{"name":"AddButton","kind":"button","instanceVars":[],"instances":[{"id":"j5","x":229,"y":184,"w":342,"h":48,"text":"Add to my journal","visible":true,"vars":{}}]},{"name":"HomeButton","kind":"button","instanceVars":[],"instances":[{"id":"dh1","x":229,"y":490,"w":342,"h":48,"text":"Home","visible":true,"vars":{}}]}],"events":[{"id":"jd1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Question","value":"\"What did you do to show \" & Chosen & \"?\""}],"children":[]},{"id":"jd2","else":false,"conds":[{"type":"onClicked","target":"AddButton"}],"actions":[{"type":"listAdd","value":"uppercase(Chosen) & \": \" & NoteInput.text","list":"Journal"},{"type":"goToScreen","screen":"journal"}],"children":[]}]},{"id":"journal","name":"Journal","objects":[{"name":"Heading","kind":"text","instanceVars":[],"instances":[{"id":"j6","x":229,"y":30,"w":342,"h":36,"text":"My values in action","visible":true,"vars":{}}]},{"name":"Count","kind":"text","instanceVars":[],"instances":[{"id":"j7","x":229,"y":72,"w":342,"h":30,"text":"","visible":true,"vars":{}}]},{"name":"List","kind":"list","instanceVars":[],"instances":[{"id":"j8","x":229,"y":110,"w":342,"h":360,"text":"","visible":true,"vars":{},"source":"Journal"}]},{"name":"HomeButton","kind":"button","instanceVars":[],"instances":[{"id":"j9","x":229,"y":490,"w":342,"h":48,"text":"Add another","visible":true,"vars":{}}]}],"events":[{"id":"jj1","else":false,"conds":[{"type":"onStart"}],"actions":[{"type":"setText","target":"Count","value":"\"Entries: \" & count(Journal)"}],"children":[]}]}],"device":"web","functions":[{"id":"fpick","name":"Pick","params":["value"],"events":[{"id":"fp1","else":false,"conds":[],"actions":[{"type":"setVar","var":"Chosen","value":"value"},{"type":"goToScreen","screen":"describe"}],"children":[]}]}],"allScreens":{"events":[{"id":"ah1","else":false,"conds":[{"type":"onClicked","target":"HomeButton"}],"actions":[{"type":"goToScreen","screen":"choose"}],"children":[]}]}}

Hand it in

Open task: any program that does what the step asks.

Stretch and support

Stretch: Make the Describe screen's AddButton event shorter with a second function, SaveEntry(text), that adds the entry and goes to the Journal screen. Is it worth it for one event? (A function earns its place when it's used more than once, or when its name makes the event easier to read.)

Support: Do Tidy 1 together for Kindness on the board, then let pairs do the other three buttons.

Exit ticket

A quiz app has five answer buttons. Each one's event adds 1 to Score, plays a sound and goes to the next question. How could you tidy it, and how would you know you hadn't broken it?

Make one function, such as Answer(), with those three actions, and have each button call it. Then play the same quiz before and after and check the score, sound and next question are the same.

What each step is for

Where it leads

Next: Creature scores, functions that give back a value. Then the challenge: encourage a tidy-up pass before they test with a real user.