@@ -20,15 +20,15 @@ happen to crawl into our work. In reality, of course, we put them
2020there ourselves.
2121
2222If 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
2424mistakes introduced while converting a thought to code. The former
2525type 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
3232computer, if it knew enough about what we're trying to do. But here
3333JavaScript's looseness is a hindrance. Its concept of bindings and
3434properties 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
5050But 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,
5252convinced that it's doing something meaningful. The mistake will
5353manifest itself only later, after the bogus value has traveled through
5454several 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
6868mode_ . This is done by putting the string ` "use strict" ` at the top of
6969a file or a function body. Here's an example:
7070
@@ -84,7 +84,7 @@ canYouSpotTheProblem();
8484
8585Normally, when you forget to put ` let ` in front of your binding, as
8686with ` 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
8888instead. This is very helpful. It should be noted, though, that this
8989doesn't work when the binding in question already exists as a global
9090binding. 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
157157goes into or comes out of a function. If you have that information
158158written 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
201201seeing whether it does the right thing.
202202
203203Doing 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
205205time to exhaustively test everything every time you make a change.
206206
207207Computers are good at repetitive tasks, and testing is the ideal
208208repetitive task. Automated testing is the process of writing a program
209209that 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
211211superpower: it takes you only a few seconds to verify that your
212212program still behaves properly in all the situations you wrote tests
213213for. 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
261261because it misbehaves or produces errors, the next step is to figure
262262out _ what_ the problem is.
263263
@@ -307,7 +307,7 @@ out why.
307307{{index "trial and error"}}
308308
309309This 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
311311what is happening and come up with a ((theory)) of why it might be
312312happening. Then, make additional observations to test this theory—or,
313313if 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
423423The 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,
425425it has to check 10 times whether ` null ` was returned. And if its
426426response to finding ` null ` is to simply return ` null ` itself, callers
427427of 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
440440into a problem to _ raise_ (or _ throw_ ) an exception. An exception can
441441be any value. Raising one somewhat resembles a super-charged return
442442from 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
444444started the current execution. This is called _ ((unwinding the
445445stack))_ . You may remember the stack of function calls that was
446446mentioned 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
453453up 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
455455down. 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
458458Here's an example:
459459
@@ -507,7 +507,7 @@ the failing call.
507507
508508Note 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
511511the error occurs and at the point where it is handled. The functions
512512in between can forget all about it.
513513
@@ -522,7 +522,7 @@ action that might cause an exception, which is pretty much every
522522function call and property access, might cause control to suddenly
523523leave 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
527527exception 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
556556to another, asking for the name of the other account in the process.
557557If 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_
560560calls ` getAccount ` before it adds it to another account. If it is
561561broken off by an exception at that point, it'll just make the money
562562disappear.
@@ -602,14 +602,14 @@ This version of the function tracks its progress, and if, when
602602leaving, it notices that it was aborted at a point where it had
603603created 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
606606is thrown in the ` try ` block, it does not interfere with the exception.
607607After the ` finally ` block runs, the stack continues unwinding.
608608
609609{{index "exception safety"}}
610610
611611Writing 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
613613because exceptions are typically reserved for exceptional
614614circumstances, the problem may occur so rarely that it is never even
615615noticed. 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
653653When 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_
655655exception it caused.
656656
657657{{index "exception handling"}}
@@ -689,7 +689,7 @@ valid direction is given. _But_ we misspelled `promptDirection`, which
689689will result in an "undefined variable" error. Because the ` catch `
690690block completely ignores its exception value (` e ` ), assuming it knows
691691what 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
695695As 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
769769the 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
772772If, for example, ` firstElement ` is described as a function that should
773773never be called on empty arrays, we might write it like this:
@@ -786,7 +786,7 @@ function firstElement(array) {
786786Now, instead of silently returning undefined (which you get when
787787reading an array property that does not exist), this will loudly blow
788788up 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
790790they occur.
791791
792792I 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
825825exception of type ` MultiplicatorUnitFailure ` . Write a function that
826826wraps this clunky function and just keeps trying until a call
827827succeeds, after which it returns the result.
0 commit comments