# One Decision, One Screen, the Right Number

Class 23, Chapter 12: Build Your Own Performance App. 27 slides, about 34.5 minutes.

## 1. Title

Slide up while people sit down. For eleven weeks you have added one step a week to a small web page. Starting today, that page becomes a tool someone else can use. A coach has asked us for something, and the first lesson is how small it should be.

## 2. The coach's request  (1.5 min)

Here is the request that runs through the whole chapter. A cross-country coach: my runners record their heart in the morning, the files sit on their phones, I just want to know whether this morning's file is good enough to use and what the number is. Notice how small that is. Most student apps fail the same way: someone asks a small question, and the builder answers ten questions nobody asked. Each extra number needs a test, a label, an explanation, and someone who wants it. So we build the smallest tool that answers the one question, and we call it Resting heart check.

## 3. A one-page specification  (2.5 min)

A specification is a short written description of what the tool must do, written before any code. It fits on one page, and it saves time twice: it tells the assistant what to build, and it tells you when to stop. Read two lines twice. The test line holds a worked answer before any code exists; that one line catches more errors than any amount of reading code. And the last line, will not do, is where scope creep stops. When a teammate says it would be cool if it also predicted injuries, the spec already says no, and why. The rule: if you cannot fill a line, you are not ready to ask for code.

## 4. What the app measures  (1.5 min)

A short reminder of what the app measures, from Chapter 5. Each heartbeat has a tall spike, the R wave. The time from one R wave to the next is an RR interval, about a thousand milliseconds at sixty beats a minute. The intervals are not equal: when you breathe in they shorten, when you breathe out they lengthen. RMSSD measures that beat-to-beat change: take each difference between neighbors, square it, average, take the root. Athletes track it, often as its logarithm, as a weekly average against their own baseline, because one day moves a lot. For the app you need the formula and one rule: compare only with the same person, measured the same way.

## 5. Six parts, and where the file goes  (2.5 min)

A browser app is a set of plain files that a web server hands out unchanged; the browser does all the work. Six parts. The page holds text and buttons. The script reacts to what the user does. The worker is a second script on its own thread, so a long calculation does not freeze the page. The canvas is a rectangle of pixels the script draws charts on. The build tool gathers the files into a small folder. And the static host hands out files and nothing else. Follow the athlete's file on the right: phone storage, to the page, to the worker and back. It never goes to the server, because there is nothing on the server that could receive it.

## 6. Messages between the page and the worker  (2 min)

Here is the page's half. The first line starts the worker in the form the build tool recommends. When a file is chosen, the page reads it into a string inside the browser and posts it to the worker. When the reply comes back, one of two things happens. If the file could not be read, the page shows why, in words, and hides the results, so an old file's numbers never sit under a new error. Otherwise it fills in the numbers and the chart. Notice that nothing here names a server. And notice the split: the worker cannot draw and the page cannot compute, so each can be tested alone.

## 7. Two real apps, the same parts  (2 min)

Two real apps you have used all term have the same parts, arranged for their input. The Heart app takes chest-strap files, does all parsing and analysis in a worker, sends progress messages to a progress bar, and draws more than twenty charts by hand. It writes a PDF and a text summary made for pasting into an assistant. The jump app takes a video, reads the true frame rate from the file, runs a pose model in the browser, and turns keypoints into numbers with plain functions. Try it tonight: open the Heart app, load a file, and watch the Network panel. No request carries the file.

## 8. The app freezes for three seconds  (2 min)

[Thirty seconds, then hands.] The answer is B. The page has one thread, and it does both the calculation and every update of the screen. While it computes, the progress bar cannot move. A worker runs the calculation on a background thread and posts the result back, so the page stays responsive and the numbers are identical. A chart library changes how you draw, not where the analysis runs. A server would free the page too, but now an athlete's heart file leaves the phone, and you have a server to run. Advanced mode changes what is shown, nothing else.

## 9. Eight requests, eight checks  (2.5 min)

You build with an assistant, as you have all term. It types fast and confidently and is sometimes wrong. Your job is to plan, check and decide. Ask for a plan first: list the files and what each does, no code yet. If the plan adds a server, an account system or a library you did not ask for, say no now, while it costs nothing. Then small steps: one request, one change you can see. The table is how this chapter's app was built, eight requests, and after each one a person checked something. Notice that the check is never: the code looks fine. If three requests in a row fail to fix the same fault, go back to the last working version and ask again, smaller.

## 10. Write the test first: two known answers  (2 min)

Write the test before you ask for the function. A known-answer test runs the code on an input whose right answer you worked out another way, by hand or with another program. Here are the first two of the student app's five. Five clean beats must give the root of four hundred fifty, twenty-one point two one milliseconds, and seventy-four point six three beats a minute. And a missed beat must be removed, and the pairs that touch it skipped. Node runs these with its built-in test runner, so there is nothing to install. The code is on the class page; open it in the lab and run it before you change anything.

## 11. All tests passed, and the chart was blank  (1.5 min)

A true story from building this chapter's app. All five tests passed. The numbers were right. And the chart was blank. The page drew the chart while its section was still hidden, and a hidden canvas measures zero pixels wide, so the chart was drawn into nothing. No test could see it, because the tests check numbers. One look at a screenshot found it, and the fix was to swap two lines: show the section first, then draw. On the left is the app as it should look. So after every step: read the diff, and then look at the screen, on a phone as well as your laptop.

## 12. Check the AI: a test that agrees with the bug  (2 min)

Here is a trap assistants fall into often. You ask for a test. The assistant runs the function, sees twenty-one point two one three two, and writes a test that expects twenty-one point two one three two. It passes. [Ask: what is wrong? Then click.] The expected value came from the code under test. If the function were wrong, the test would be wrong in exactly the same way, and it would still pass. A test like that can only ever confirm the code. The known answer must come from somewhere else: your hand calculation, a spreadsheet, or an independent program.

## 13. Check it by hand: RMSSD from five beats  (2.5 min)

[Everyone computes this now. Two minutes.] Five intervals: eight hundred, eight twenty, seven ninety, eight ten, eight hundred. [Click.] Four differences: twenty, minus thirty, twenty, minus ten. Squares add to eighteen hundred. Divide by four, the number of differences, not five: four hundred fifty. The root is twenty-one point two one. Heart rate: the mean interval is eight hundred four, so sixty thousand over that is seventy-four point six three. Now the two easy mistakes. Divide by five and you get eighteen point nine seven, eleven percent low, and nobody would notice by looking. That is why the hand answer goes into the test.

## 14. Synthetic data with planted faults  (2.5 min)

A hand calculation tests five numbers; a real file has hundreds. Synthetic data is data you make so that you know the answer exactly. This file has three hundred thirty-one intervals with breathing-driven variation, and three faults a real strap produces: a missed beat, where two intervals are read as one, and a split beat, where one interval is read as two. The app should remove exactly those three, the circles, and keep everything else. Then a third check: a Python program written without looking at the JavaScript. Both say three removed, thirty-nine point zero nine milliseconds, sixty-one point five four beats a minute. A test now holds the app to those values.

## 15. One missed beat, three answers  (2.5 min)

[Take guesses before you click.] One missed beat in five intervals. Without cleaning, RMSSD is five hundred sixty-two milliseconds: one fault multiplies it many times over. Cleaning matters enormously. Then there are two defensible ways to clean. The student app skips the pairs that touch the removed beat: fifteen point eight one. The Heart app joins the neighbors across the gap: fourteen point one four. Neither is a bug. On the real-length example file they differ by only zero point one six, because faults are rare. So: always clean, and write down which rule you chose, so a user comparing with another tool knows why the numbers differ.

## 16. Three inputs every app must survive  (2 min)

All the checks so far feed the app a good file. Users will not. So every app you build gets a second, required set of tests, one per kind of input. Normal: the right answer. Missing: no number, and a message saying what is missing. Invalid: no number, and a message saying how to fix it. Write the expected behavior before the test. It does not crash is not enough. This set earned its place on its first run: a file in seconds passed the old guard, every beat was cleaned away, and the page printed not-a-number in the big font. And looking at the screen found a second fault: an old file's numbers left under the new error.

## 17. Right formula, still a poor measure?  (1.5 min)

The app now computes RMSSD correctly: it matches the formula and an independent program. That is not the same as the number being a good measure of the runner. Four sources of error sit outside the code. Artifacts, which the summary handles by stating the share removed. The cleaning choice, which you state. Conditions: lying or sitting, time of day, coffee. The app can only tell users to record the same way every time. And day-to-day variation, Chapter 11's typical error. That last one is why the spec says no readiness verdict: a single morning's value cannot tell an athlete to train or rest.

## 18. Before Class 24  (1.5 min)

Project step twenty-four is due at the start of next class: a prototype. Your app working end to end on sample input, with the three tests we just saw, normal, missing and invalid, passing. The exercises are on the slide. Twelve point five is six RR intervals by hand; it is the hand answer for your own normal test. Twelve point eight runs the jump app on yourself and then reads it as a builder. Checkpoint three of the team project goes in with it. Bring your app's link as a QR code. Read sections twelve point five to twelve point eleven before Thursday: privacy, reports, user tests and putting it online.
