Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Trace an Order From Click to File

In this video, you traced one Small Maps order with READ20, for Nadia Patel, from the click on Place order to what The Second Draft stores, and checked the trace with one run.

Key Points

  • A trace starts where the user acts. A search for the button’s label, Place order, finds public/admin/index.html. The form there has the id order-form, the name the page’s JavaScript finds it by. A search for that id leads to line 73 of public/admin/app.js, where #order-form marks it as an id.
  • The staff desk page runs in your browser and sends the form to the server at /api/orders. The server sends the result back as its reply.
  • Follow the code in the order it runs, and write each step down with its file and function. A function you haven’t opened, such as handleForm(), runCommand() or json(), is a black box. Describe it from its name, arguments and folder. Mark what you assume it changes, like the files runCommand() writes, as an assumption until a run checks it.
  • At every step, ask: does it write, send or schedule anything? A trace that only follows the reply back to Saved order-2 misses most of what createOrder() changes. Those changes happen in memory, through state, and reach a file only when runCommand() writes them.
  • A trace you’ve only read is still a prediction. Add a console.log() line at the steps and run the feature once. A log line shows that a step ran. The stored data shows what it changed.

The Trace

Here’s the entry the video added to your map. Each step names its file and function, then what it changes, in the order the code runs when staff click Place order. Step four started as an assumption about runCommand(), a function you haven’t opened, and the run turned it into a checked fact:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your map says what priceOrder() does with READ20. It doesn’t say how a click gets there, or what else an order changes. Grace’s last question, how a discount code gets applied to an order, needs that whole path, through several files.

00:15 Here, you’ll trace it once, writing each step down. The discount’s part comes later. Take your first-hour order: Small Maps with READ20, for Nadia Patel. Click Place order, and Saved order-2 appears under the form.

00:31 A trace starts where the user acts, at the button. Search for its label, and the only match is in index.html, the staff desk’s page. Its form has the id order-form, the name the page’s JavaScript uses to find it.

00:47 Search for that, and it leads into app.js, where a hash sign marks it as an id.

00:54 That line passes the form and the address /api/orders to handleForm(). You haven’t opened it, so treat it as a black box. Its name and arguments say it sends the form there.

01:06 The rest of the call picks which fields to send. It’s an arrow function you don’t need to read yet. So step one is app.js, with the order form’s handler.

01:18 That page runs in your browser, and the address leads to the server. Search for it, and the second match is in server.js, the file npm start runs. That’s the branch for requests to that address.

01:32 The next line calls createOrder() with what the form sent. Ignore its await and arrow for now. runCommand() is another black box, from the storage folder.

01:42 So assume it loads the shop’s file, runs createOrder(), then writes the results to files. Mark that as an assumption for the run to check. Step two is the branch that hands the order on.

01:55 Further down, json(), another black box, sends back the reply.

02:02 Step three is createOrder(). You’ve read where it prices the order and queues the confirmation. Watch out here. A trace that only follows the reply back to the form misses most of this function.

02:15 So at every step, ask: does it write, send or schedule anything? These lines reserve the copy, pay, and release the reservation. The name says release. Assume its true also takes the copy off the shelf, as the 18 to 17 you saw suggests.

02:33 Then it adds the order and records an order.paid event. All of that changes the shop in memory, through state, and nothing reaches a file yet. That’s step three.

02:44 Step four is runCommand(), which you assume writes the files. Step five, server.js sends the reply. Step six, app.js shows Saved order-2.

02:58 Your trace comes from reading, so it’s still a prediction. To check it, add a console.log() line at steps two, three and four, two in server.js and one in createOrder().

03:11 Reseed and restart the shop, and place the same order. Step three’s line logs the order’s id and total. Your turn: pause the video and predict what that line prints.

03:28 It prints order-2 and 600, your first hour’s 600 pence.

03:34 The three lines arrive in the order you wrote the steps. But a log line only shows that a step ran, not what it changed. So check the files. The reseed left only order-1 and no emails.

03:48 Now searching data/shop.json finds order-2, and the outbox holds email-1.json. Step four’s assumption checks out.

03:58 Then delete those three lines, to leave the team’s code unchanged.

04:04 So to trace a feature, start from what the user does and follow it file by file, in the order it runs. Write down each step’s file, function and what it changes.

04:15 Then confirm it with a run.

04:18 Add the trace to your map. Next comes Exercise: Put the Trace in Order, where you’ll write this order’s steps and check them against a recorded run.

Become a Member to join the conversation.