Learn Practice Daily golf Reference
Operators·Lesson 6 of 7·8 min·0/9 exercisesPro

Indent & case

Verbs that change the shape, not the words: >> and << shift, = tidies by the braces, and gU gu g~ change case with any noun.

Scene

Wednesday, 8:55 a.m. Somebody switched the Formatter back on yesterday, "just for one file". It ran at 3 a.m. over every file, and it had opinions. Half of kitchen.ts lost its indentation, and a few names are now in CAPITAL LETTERS, as if the file were shouting at you.

The letters are all still there; they're just the wrong shape. In code, shape still matters: ENQUEUE and enqueue are different names, and only one of them is called anywhere.

From memory

Warm-up from earlier chapters

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

Repeat the last change, with 5 as its new count.
press the keys…2 keys
1 of 2
Repeat the last change, with 5 as its new count.

Verbs that change the shape

Some verbs change how text looks, not what it says. > shifts lines one indent to the right and < shifts them left. Say them twice for the line you're on: >> indents it, << takes an indent off. With a count, 3>> shifts three lines.

Before
export class Kitchen {
private busy = false;
}
>>
export class Kitchen {
private busy = false;
}
>> shifts the line one indent right, from anywhere on it.
Try it

Shift it back

Each marked line has one indent too few or too many. >> or << from anywhere on it.

1 of 4Type ·· where it's shown in green
1export class Kitchen {
1 private queue: Order[] = [];
2 private busy = false;
3
4enqueue(order: Order) {
5 this.queue.push(order);
6 this.startNext();
7 }
8
9startNext() {
10 if (this.busy || this.queue.length === 0) return;
11 this.busy = true;
12 const next = this.queue.shift();
13 cook(next).then(() => this.finish());
14 }
15}
NORMALLoading the editor…1/40.0s1:1

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

= tidies up

= is the verb tidy: it re-indents lines to match the code's structure, so you don't have to count indents. == fixes the line you're on, and 3== three lines from here. Inside a function, a flattened line goes back in one level per brace.

1export function wake(k: Kitchen) {
2k.open();
1k.display.warmUp();
2return k;
3}
NORMALLoading the editor…2:1
Check yourself

Shift or tidy?

1export function nextTicket(queue: Order[]) {
2if (queue.length === 0) {
1return null;
2}
3return queue.shift();
4}
NORMAL
Keys pressed: 4>>

The Formatter flattened this whole function. From the if, you press 4>>. Is it fixed?

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

Tidy the function

Same function, still flat. One sentence with = from the if: every line goes to the depth its braces ask for.

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.

Case is a verb too

gU is uppercase, gu is lowercase and g~ flips the case. They take any noun: gUiw shouts the word under the cursor, guiw calms it down. ~ on its own flips just the character under the cursor and moves on, so ~~~ walks along flipping.

Before
ENQUEUE(order: Order) {
guiw
enqueue(order: Order) {
guiw from the middle of the word: the whole word goes lowercase.
Check yourself

Which part changes?

1const max_qty = 12;
NORMAL
Keys pressed: gUiw

The cursor is on the q of max_qty. You press gUiw. What does the line say?

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

Stop shouting

Some names were shouted, some constants whispered. guiw or gUiw from anywhere in the word.

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

Shape verbs

From memory.

Indent this line.
scroll here, then press the keys2 keys
1 of 5
Indent this line.
Challenge

Formatter cleanup II

Lost indents and shouted names across the codebase. The letters are right; their case and indent are wrong. Shape verbs only.

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

Calm the class

The top of kitchen.ts. max_rush is a constant and should be in capitals; the two fields go back in one level, where class members belong; and ENQUEUE should be the method name the rest of the code calls, enqueue. Then open the Formatter's own config and stop this happening again: indent by 2, and no more shouting (shout: false).

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.

Extra practice optional

The chapter is done. These rounds keep going as long as you like: stop whenever it stops being useful.

Endless

Formatter aftershocks

Endless indents and case to put right.

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.