Loading video player…

Search for What You Can See

In this video, you followed the confirmation your Small Maps order wrote to the outbox back to the code that writes its words, listed where that code is defined and used, and followed one of its imports outward. Every search below runs in a terminal inside the second-draft-books folder, so you can type along.

Search for the Words in the Outbox

Here’s the confirmation your order wrote to outbox/email-1.json:

Language: JSON
{
  "id": "email-1",
  "to": "[email protected]",
  "subject": "Your order order-2",
  "text": "Thanks for visiting The Second Draft. Total paid: £6.00."
}
  • Some line of code produced those words, so search for them. Don’t search for the whole sentence. £6.00 is your order’s total, and another order shows another amount, so no source file holds that figure as typed. The code builds it for each order. Pick a piece every confirmation shares, such as Thanks for visiting.
  • git grep searches every file Git tracks, meaning the files saved in the repository, not the ones the shop writes while it runs. -n adds line numbers. -F searches for exactly the characters you typed.
Language: Shell
$ git grep -n -F "Thanks for visiting"
src/email/templates.js:6:        text: 'Thanks for visiting The Second Draft.
⮑ Total paid: ' + formatPrice(order.total) + '.'

Each result is a file path, a line number and the matching line. The terminal prints this one on a single line, and the ⮑ marks where it’s wrapped here.

The match sits inside orderMessage(), the function createOrder() calls. Its text is joined from the fixed wording, formatPrice(order.total), and a closing '.':

Language: JavaScript
import { formatPrice } from '../lib/money.js';

export function orderMessage(order) {
    return {
        subject: 'Your order ' + order.id,
        text: 'Thanks for visiting The Second Draft. Total paid: '
            + formatPrice(order.total) + '.'
    };
}

Line 6 is one line in the file, wrapped here after its first +.

  • The total only exists once there’s an order. The finished sentence is only in files the shop writes as it runs, outbox/email-1.json and data/shop.json. Git doesn’t track them, so git grep never sees them.
  • Search for the whole sentence, and nothing comes back, though the code is right there in templates.js. No results is a fact about your search, not the code. Try a shorter piece, or add -i to ignore case, before you decide something isn’t there.

If a search of yours comes back empty, this button asks Code Mentor what to try next. Paste the search you ran:

You’ve now reached orderMessage() from both ends, down from createOrder() in Grace’s notes and up from what the customer reads. Two routes that agree are better evidence than one.

List Where a Function Is Defined and Used

Before Grace’s wording changes, check what else uses orderMessage(). If another file calls it, that file’s message changes too:

Language: Shell
$ git grep -n -F "orderMessage"
src/email/templates.js:3:export function orderMessage(order) {
src/orders/create.js:11:import { orderMessage } from '../email/templates.js';
src/orders/create.js:27:    queueEmail(state, customer, orderMessage(order));

The line with export function is the definition. The other two are its uses, both in src/orders/create.js. Line 11 imports it, and line 27 calls it. So orderMessage() is called in one place, the order confirmation.

Follow the Import Outward

Line 1 of templates.js imports formatPrice(), the function that formats the total, from money.js in the lib folder. A search for its name lists 11 lines, including Grace’s notes in docs/FIRST-HOUR.md and three lines of the tests in test/pricing.test.js, which import and check it.

For now, you want the shop’s own code. Add -- and then src, the source folder. Whatever follows the two dashes says where to search, so git grep leaves out every file outside src/:

Language: Shell
$ git grep -n -F "formatPrice" -- src
src/email/templates.js:1:import { formatPrice } from '../lib/money.js';
src/email/templates.js:6:        text: 'Thanks for visiting The Second Draft.
⮑ Total paid: ' + formatPrice(order.total) + '.'
src/email/templates.js:13:    return { subject: 'Refund for ' + order.id,
⮑ text: lead + formatPrice(refund.amount) + tail };
src/lib/money.js:9:export function formatPrice(pence) {
src/reports/csv.js:1:export function formatPrice(pence) {
src/reports/csv.js:8:        output += row.orderId + ',' + row.date + ','
⮑ + formatPrice(row.paid) + ','
src/reports/csv.js:9:            + formatPrice(row.refunded) + ','
⮑ + formatPrice(row.net) + '\n';

To narrow it further, search for the words that start a definition:

Language: Shell
$ git grep -n -F "function formatPrice"
src/lib/money.js:9:export function formatPrice(pence) {
src/reports/csv.js:1:export function formatPrice(pence) {

These are two different functions with the same name. A text search can’t tell which one a call reaches. In templates.js, the import line tells you which one the confirmation reaches. An editor’s Go to Definition and Find All References follow that import line for you, so they tell the two apart.

If a search lists more lines than you want to read, this button asks Code Mentor how to narrow it. Add the command you ran and what you were looking for:

Your Map

Your first entry gains three lines:

Language: Markdown Text
- Wording: orderMessage() in src/email/templates.js,
  found by searching "Thanks for visiting"
- Called once: src/orders/create.js line 27
  (imported on line 11)
- Open question: two functions are called
  formatPrice(). Which output uses which?
  The import lines will say.

When you can see some text, search for the piece that never changes. Then follow the definition, its uses and its imports, one step at a time, and write down what you find.

Next comes Exercise: Track Down the Two Prices, where you’ll answer Grace’s question about why two prices look different, and find every place each one is used.

00:00 Your first hour ended at one line of createOrder(). It calls orderMessage() to build the confirmation. But where are the words themselves written?

00:08 If Grace wants the wording changed, that’s the line someone has to find.

00:14 This time, Grace’s notes showed you where to start. Most questions come without notes, but with something a person saw. Here, that’s the confirmation your Small Maps order wrote to the outbox.

00:25 Some line of code produced those words, so search for them.

00:31 Don’t search for the whole sentence. The six pounds is your order’s total, and another order shows another amount. So no source file holds that figure as typed.

00:40 The code builds it for each order. Pick a piece every confirmation shares, like the words Thanks for visiting at the start. In a terminal, inside the shop’s folder, type git grep.

00:53 It searches every file Git tracks, meaning the files saved in the repository, not the ones the shop writes while it runs. -n adds line numbers.

01:03 -F searches for exactly the characters you typed. One match comes back: line 6 of templates.js, in the email folder.

01:16 Open templates.js, and the match sits inside orderMessage(), the function createOrder() calls. Its text is joined from three pieces.

01:27 There’s the fixed wording, then formatPrice() of the order’s total, then a closing dot. The total only exists once there’s an order. The finished sentence is only in files the shop writes as it runs, like the outbox file.

01:42 Git doesn’t track them, so git grep never sees them.

01:46 Search for the whole sentence, and nothing comes back, though the code is right here. No results is a fact about your search, not the code. Try a shorter piece, or add -i to ignore case, before you decide it isn’t there.

02:02 You’ve now reached orderMessage() from both ends. Grace’s notes led you down from createOrder(), and the outbox led you up. Two routes that agree are better evidence than one.

02:13 Note that in your map. Before Grace’s wording changes, check what else uses this function. If another file calls it, that file’s message changes too. Search for its name.

02:25 Three lines. The one with export function is the definition. The other two are its uses, both in create.js. Line 11 imports it, and line 27 calls it.

02:39 So orderMessage() is called in one place, the order confirmation. Add that to your map too.

02:46 Now follow the import on line 1. It brings in formatPrice(), the function that formats the total, from money.js in the lib folder. Search for its name.

02:57 Eleven lines. Some are Grace’s notes, and three are the tests, which import and check it. For now, you want the shop’s own code. Add -- src, the source folder.

03:11 Whatever follows the dashes says where to search, so files outside src are left out. Seven lines. To narrow it further, search for function formatPrice, the words that start a definition.

03:26 Two definitions come back, one in money.js and one in the reports folder. They’re two different functions with the same name. A text search can’t tell which one a call reaches.

03:38 In templates.js, the import line tells you which one the confirmation reaches. An editor’s Go to Definition and Find All References follow that import line for you, so they tell the two apart.

03:51 So add a question to your map: which output uses which one?

03:57 So when you can see some text, search for the piece that never changes. Then follow the definition, its uses and its imports, one step at a time, and write down what you find.

04:10 Next comes Exercise: Track Down the Two Prices, where you’ll answer Grace’s question about why two prices look different, and find every place each one is used.

Become a Member to join the conversation.