Skip to content

Commit 56c57a2

Browse files
committed
Integrate copyediting for Chapter 8
1 parent 6f95dd3 commit 56c57a2

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

08_error.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ happen to crawl into our work. In reality, of course, we put them
2020
there ourselves.
2121

2222
If a program is crystallized thought, you can roughly categorize bugs
23-
into those caused by the thoughts being confused, and those caused by
23+
into those caused by the thoughts being confused and those caused by
2424
mistakes introduced while converting a thought to code. The former
2525
type is generally harder to diagnose and fix than the latter.
2626

2727
## Language
2828

2929
{{index parsing, analysis}}
3030

31-
Many mistakes could automatically be pointed out to us by the
31+
Many mistakes could be pointed out to us automatically by the
3232
computer, if it knew enough about what we're trying to do. But here
3333
JavaScript's looseness is a hindrance. Its concept of bindings and
3434
properties is vague enough that it will rarely catch ((typo))s before
@@ -48,7 +48,7 @@ program tries to perform the action.
4848
{{index NaN, error}}
4949

5050
But often, your nonsense computation will merely produce `NaN` (not a
51-
number) or an undefined value. And the program happily continues,
51+
number) or an undefined value, while the program happily continues,
5252
convinced that it's doing something meaningful. The mistake will
5353
manifest itself only later, after the bogus value has traveled through
5454
several functions. It might not trigger an error at all but silently
@@ -64,7 +64,7 @@ _((debugging))_.
6464

6565
{{indexsee "use strict", "strict mode"}}
6666

67-
JavaScript can be made a _little_ more strict by enabling _strict
67+
JavaScript can be made a _little_ stricter by enabling _strict
6868
mode_. This is done by putting the string `"use strict"` at the top of
6969
a file or a function body. Here's an example:
7070

@@ -84,7 +84,7 @@ canYouSpotTheProblem();
8484

8585
Normally, when you forget to put `let` in front of your binding, as
8686
with `counter` in the example, JavaScript quietly creates a global
87-
binding and uses that. In strict mode an ((error)) is reported
87+
binding and uses that. In strict mode, an ((error)) is reported
8888
instead. This is very helpful. It should be noted, though, that this
8989
doesn't work when the binding in question already exists as a global
9090
binding. In that case, the loop will still quietly overwrite the value
@@ -157,8 +157,8 @@ lot of mistakes come from being confused about the kind of value that
157157
goes into or comes out of a function. If you have that information
158158
written down, you're less likely to get confused.
159159

160-
You could add a comment like this above the `goalOrientedRobot`
161-
function from the last chapter, to describe its type.
160+
You could add a comment like the following before the `goalOrientedRobot`
161+
function from the previous chapter to describe its type:
162162

163163
```
164164
// (VillageState, Array) → {direction: string, memory: Array}
@@ -201,13 +201,13 @@ we'll have to find them the hard way: by running the program and
201201
seeing whether it does the right thing.
202202

203203
Doing this by hand, again and again, is a really bad idea. Not only is
204-
it annoying, it also tends to be ineffective, since it takes too much
204+
it annoying, it also tends to be ineffective since it takes too much
205205
time to exhaustively test everything every time you make a change.
206206

207207
Computers are good at repetitive tasks, and testing is the ideal
208208
repetitive task. Automated testing is the process of writing a program
209209
that tests another program. Writing tests is a bit more work than
210-
testing manually, but once you've done it you gain a kind of
210+
testing manually, but once you've done it, you gain a kind of
211211
superpower: it takes you only a few seconds to verify that your
212212
program still behaves properly in all the situations you wrote tests
213213
for. When you break something, you'll immediately notice, rather than
@@ -257,7 +257,7 @@ values rather than changing objects, tends to be easy to test.
257257

258258
{{index debugging}}
259259

260-
Once you notice that there is something wrong with your program
260+
Once you notice there is something wrong with your program
261261
because it misbehaves or produces errors, the next step is to figure
262262
out _what_ the problem is.
263263

@@ -307,7 +307,7 @@ out why.
307307
{{index "trial and error"}}
308308

309309
This is where you must resist the urge to start making random changes
310-
to the code to see if that makes it better. Instead, _think_. Analyze
310+
to the code to see whether that makes it better. Instead, _think_. Analyze
311311
what is happening and come up with a ((theory)) of why it might be
312312
happening. Then, make additional observations to test this theory—or,
313313
if you don't yet have a theory, make additional observations to help
@@ -421,7 +421,7 @@ function lastElement(array) {
421421
{{index "special return value", readability}}
422422

423423
The second issue with returning special values is that it can lead to
424-
very awkward code. If a piece of code calls `promptNumber` 10 times,
424+
awkward code. If a piece of code calls `promptNumber` 10 times,
425425
it has to check 10 times whether `null` was returned. And if its
426426
response to finding `null` is to simply return `null` itself, callers
427427
of the function will in turn have to check for it, and so on.
@@ -440,7 +440,7 @@ Exceptions are a mechanism that makes it possible for code that runs
440440
into a problem to _raise_ (or _throw_) an exception. An exception can
441441
be any value. Raising one somewhat resembles a super-charged return
442442
from a function: it jumps out of not just the current function but
443-
also out of its callers, all the way down to the first call that
443+
also its callers, all the way down to the first call that
444444
started the current execution. This is called _((unwinding the
445445
stack))_. You may remember the stack of function calls that was
446446
mentioned in [Chapter ?](functions#stack). An exception zooms down
@@ -453,7 +453,7 @@ they would not be of much use. They'd just provide a novel way to blow
453453
up your program. Their power lies in the fact that you can set
454454
"obstacles" along the stack to _catch_ the exception as it is zooming
455455
down. Once you've caught an exception, you can do something with it to
456-
address the problem, and then continue to run the program.
456+
address the problem and then continue to run the program.
457457

458458
Here's an example:
459459

@@ -507,7 +507,7 @@ the failing call.
507507

508508
Note that the `look` function completely ignores the possibility that
509509
`promptDirection` might go wrong. This is the big advantage of
510-
exceptions: Error-handling code is necessary only at the point where
510+
exceptions: error-handling code is necessary only at the point where
511511
the error occurs and at the point where it is handled. The functions
512512
in between can forget all about it.
513513

@@ -522,7 +522,7 @@ action that might cause an exception, which is pretty much every
522522
function call and property access, might cause control to suddenly
523523
leave your code.
524524

525-
That means that when code has several side effects, even if its
525+
This means when code has several side effects, even if its
526526
"regular" control flow looks like they'll always all happen, an
527527
exception might prevent some of them from taking place.
528528

@@ -556,7 +556,7 @@ The `transfer` function transfers a sum of money from a given account
556556
to another, asking for the name of the other account in the process.
557557
If given an invalid account name, `getAccount` throws an exception.
558558

559-
But `transfer` _first_ removes the money from the account, and _then_
559+
But `transfer` _first_ removes the money from the account and _then_
560560
calls `getAccount` before it adds it to another account. If it is
561561
broken off by an exception at that point, it'll just make the money
562562
disappear.
@@ -602,14 +602,14 @@ This version of the function tracks its progress, and if, when
602602
leaving, it notices that it was aborted at a point where it had
603603
created an inconsistent program state, it repairs the damage it did.
604604

605-
Note that, even though the `finally` code is run when an exception
605+
Note that even though the `finally` code is run when an exception
606606
is thrown in the `try` block, it does not interfere with the exception.
607607
After the `finally` block runs, the stack continues unwinding.
608608

609609
{{index "exception safety"}}
610610

611611
Writing programs that operate reliably even when exceptions pop up in
612-
unexpected places is very hard. Many people simply don't bother, and
612+
unexpected places is hard. Many people simply don't bother, and
613613
because exceptions are typically reserved for exceptional
614614
circumstances, the problem may occur so rarely that it is never even
615615
noticed. Whether that is a good thing or a really bad thing depends on
@@ -651,7 +651,7 @@ Such exceptions can also be caught.
651651
{{index "catch keyword"}}
652652

653653
When a `catch` body is entered, all we know is that _something_ in our
654-
`try` body caused an exception. But we don't know _what_, or _which_
654+
`try` body caused an exception. But we don't know _what_ did or _which_
655655
exception it caused.
656656

657657
{{index "exception handling"}}
@@ -689,7 +689,7 @@ valid direction is given. _But_ we misspelled `promptDirection`, which
689689
will result in an "undefined variable" error. Because the `catch`
690690
block completely ignores its exception value (`e`), assuming it knows
691691
what the problem is, it wrongly treats the binding error as indicating
692-
bad input. Not only does this cause an infinite loop, it also
692+
bad input. Not only does this cause an infinite loop, it
693693
"buries" the useful error message about the misspelled binding.
694694

695695
As a general rule, don't blanket-catch exceptions unless it is for the
@@ -767,7 +767,7 @@ error will be properly reported.
767767

768768
_Assertions_ are checks inside a program that verify that something is
769769
the way it is supposed to be. They are used not to handle situations
770-
that can come up in normal operation, but to find programmer mistakes.
770+
that can come up in normal operation but to find programmer mistakes.
771771

772772
If, for example, `firstElement` is described as a function that should
773773
never be called on empty arrays, we might write it like this:
@@ -786,7 +786,7 @@ function firstElement(array) {
786786
Now, instead of silently returning undefined (which you get when
787787
reading an array property that does not exist), this will loudly blow
788788
up your program as soon as you misuse it. This makes it less likely
789-
for such mistakes to go unnoticed, and easier to find their cause when
789+
for such mistakes to go unnoticed and easier to find their cause when
790790
they occur.
791791

792792
I do not recommend trying to write assertions for every possible kind
@@ -820,8 +820,8 @@ ensure that a piece of code _always_ runs when a block finishes.
820820

821821
{{index "primitiveMultiply (exercise)", "exception handling", "throw keyword"}}
822822

823-
Say you have a function `primitiveMultiply` that, in 20 percent of
824-
cases, multiplies two numbers, and in the other 80 percent, raises an
823+
Say you have a function `primitiveMultiply` that in 20 percent of
824+
cases multiplies two numbers and in the other 80 percent of cases raises an
825825
exception of type `MultiplicatorUnitFailure`. Write a function that
826826
wraps this clunky function and just keeps trying until a call
827827
succeeds, after which it returns the result.

0 commit comments

Comments
 (0)