Learn Practice Daily golf Reference
Real-world refactors·Lesson 1 of 6·11 min·0/6 exercisesPro

Rename a concept

Launch week, Monday: orders become tickets in the kitchen module. * and cgn where you choose each one (and pass by the one that must stay), a ranged :s inside one function, :%s with \<\> for whole words.

Scene

Monday of launch week, 9:10 a.m. Burrito 2.0 calls orders tickets, in the code at least: the kitchen module has to follow before Saturday's launch. The customer-facing text still says "order", so a blind rename is out.

Six jobs this week, one a day. Today's is a rename done properly.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

Replace every order with ticket in the file.
press the keys…19 keys
1 of 2
Replace every order with ticket in the file.

Which tool for which rename

Pick by how many places and how picky: * on the name, then cgn once and . for each next match, when you want to see every one as it changes. A ranged slip, :.,+4s/\<total\>/amount/g, when the rename stays inside one function. :%s/\<Order\>/Ticket/g when the whole word goes everywhere. \< and \> keep it to whole words.

None of these knows what the code means: they match text. A string or a comment with the same name matches too, so before the first key, find what must stay.

Before
export function orderTotal(order: Order) {
return orderTotal(order.items) + "order";
*NcgnticketTotalEsc.
export function ticketTotal(order: Order) {
return ticketTotal(order.items) + "order";
The name everywhere it's code; the word inside the string is a different word and stays.
Check yourself

Whole words, in quotes

1const total = orderTotal(order);
1metrics.track("orderTotal", total);
NORMAL
Keys pressed: :%s/\<orderTotal\>/ticketTotal/g↵

The dashboard charts the metric by its name, "orderTotal", so it must stay. You send :%s/\<orderTotal\>/ticketTotal/g. What does the second line say now?

Pick an answer (or press its number), then watch the engine prove it.
Boss round

Stage 1 · The function name

9:15. orderTotal becomes ticketTotal in its three code places; the metric name in metrics.track("orderTotal", …) stays. The cursor is on the definition: * finds the next one, N comes back, cgn changes this one (type ticketTotal), and . changes the next: after cgn, the stamp finds the next match by itself. When the next match is the one that stays, n onto it, n past it, then ..

Fix the marked spots · 3 left
2import type { Order } from "./types";
1
3export function orderTotal(order: Order) {
1 const items = order.items.reduce((sum, i) => sum + i.price, 0);
2 return items + order.fee;
3}
4
5export function printOrder(order: Order) {
6 const total = orderTotal(order);
7 metrics.track("orderTotal", total);
8 return "Your order: " + total.toFixed(2);
9}
10
11export function refundOrder(order: Order) {
12 const total = orderTotal(order);
13 payments.refund(order.id, total);
14 return { ...order, refunded: total };
15}
NORMALLoading the editor…0.0s3:17

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

Stage 2 · One function only

9:30. Inside printOrder only, the local total becomes amount; the other functions keep theirs. The function is this line and the four below it.

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

Stage 3 · The type

9:40. The Order type becomes Ticket everywhere in the file. The function names printOrder and refundOrder stay for now, and so does the customer's "Your order: ".

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

From memory

Rename tools, from memory

Press the keys.

Search for the word under the cursor.
scroll here, then press the keys
1 of 3
Search for the word under the cursor.