← App Builder apps

Tidy up the journal

Refactor the values journal: change how it's built without changing what it does. Four near-identical button events become one function, Pick(value), and the back buttons on two screens become one All screens event. Run the same journey before and after to check nothing changed, then share the finished app with your teacher.

Real apps get changed for years. When the same few actions are copied in five places, a change means five edits, and missing one is a bug. Tidying them into one place is called refactoring.

The values journal from an earlier lesson works. Inside, though, some of it is written out more than once. Today you'll refactor it: change how it's built without changing what it does.

  1. Spot the repetition: jobs written out almost the same way in more than one place.
  2. Pull it into one place: a function, or an event on All screens.
  3. Check nothing changed: run the same journey before and after, and get the same result.

The values journal's three screens: four value buttons; a question with a typing box and an Add button; and a list of entries such as "COURAGE: I asked a question in class".

How this lesson works: See it, Check, Make it yours, Build
See itTry the finished thing first, so you know what you are building.

Run it and write it down

This is the finished values journal, open on the Run tab. Do this journey and write down what you see at the end:

  1. On Choose, click Courage.
  2. Type I asked a question in class and click Add to my journal.
  3. On the Journal screen, write down the entry and the count exactly as they appear.

You'll do the same journey at the end. If anything is different then, the tidy-up changed what the app does.

Try it yourselfYour edits are saved in this browser.

CheckA quick check that the idea has stuck.

Spot the repetition

Open the Choose screen, then the Events tab, and read the four button events side by side.

Open the Choose screen's Events tab. The four value buttons each have an event. What is different between them?

Make it yoursChange your app on purpose, then try it on someone.

Tidy 1: one function, Pick

  1. In the sidebar, under Functions, click + Function and name it Pick.
  2. Click + Parameter and call it value. Inside the function, value is whatever the call gives it.
  3. Add the function's actions: Variables, Set variable Chosen to value (no quote marks: it's the parameter's name, not the word "value"), then Screen, Go to screen Describe.
  4. Open the Choose screen's events. In KindnessButton is clicked, delete both actions and add Functions, Call Pick with value "Kindness".
  5. Do the same for the other three buttons, each with its own value.
  6. Run it and click each button. Does each one still go to Describe with the right question?

Now, if every value should also play a sound, that's one change in Pick, not four.

A function with a parameter · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

A function Pick(value) sets Chosen to value and goes to Describe, and each value button just calls Pick with its value.

    Run your program. This ticks when your code does all of the above.

    CheckA quick check that the idea has stuck.

    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.

    Make it yoursChange your app on purpose, then try it on someone.

    Tidy 2: one Home event for every screen

    The Describe screen has no way back. Add one the way you know:

    1. Open the Describe screen and add a Button called HomeButton, with the text Home.
    2. On Describe's Events tab: HomeButton is clicked → Go to screen Choose.

    Now look at the Journal screen's events too. AnotherButton is clicked → Go to screen Choose. That's the same job, written out a second time.

    1. On the Journal screen, rename AnotherButton to HomeButton. Keep its text as Add another: the name is for the events, the text is for the user.
    2. Click All screens in the sidebar and add one event: HomeButton is clicked → Go to screen Choose.
    3. Delete the Home events on Describe and on Journal. The All screens event does both jobs now.
    4. Open the Map tab. The arrows back to Choose now come from one event.

    An All screens event runs on every screen. On a screen with no HomeButton, such as Choose, it just never happens.

    Try it yourselfYour edits are saved in this browser.

    Describe has a HomeButton, and its own event takes you back to Choose.

      Run your program. This ticks when your code does all of the above.

      AnotherButton is now called HomeButton, and one All screens event, HomeButton is clicked, goes to Choose (with no Home events left on the screens).

        Run your program. This ticks when your code does all of the above.

        BuildWatch the short clip, then do the steps. Your app carries on from the step before.

        Same journey, same result?

        Open the Choose screen, then Run, and do the same journey as at the start: Courage, I asked a question in class, Add to my journal. Compare the Journal screen with what you wrote down.

        If something is different, the tidy-up changed what the app does. Find the event or function you changed and compare it with the old one.

        Try it yourselfYour edits are saved in this browser.

        Make it yoursChange your app on purpose, then try it on someone.

        Hand it in

        1. Click Share at the top of App Builder and copy the link.
        2. Send it to your teacher the way your class usually hands work in. The link opens your app ready to run, with Open a copy in App Builder if they want to look inside.

        If your teacher would rather have a file, Save at the top, then Save to this computer…, keeps a copy to upload instead.

        Try it yourselfYour edits are saved in this browser.

        Now you can
        Next lesson11. Creature scoresFunctions that give back a value: BarWidth(stat) works out a bar's width once, for all three bars, and Overall(power, speed, defence) gives back one score for each creature. Find out what happens when a function works something out but never gives it back.
        For teachers

        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. About 50 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        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.

        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

        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.

        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.

        What each step is for