You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/ch04.asciidoc
+26-8Lines changed: 26 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -481,18 +481,40 @@ Whenever we are uncertain about whether an abstraction is up to muster, it pays
481
481
482
482
==== 4.2.7 Slicing Large Functions
483
483
484
-
Consider breaking what would otherwise inevitably be a single large function into smaller functions. These may be organized by splitting functionality by steps, by different aspects of the same task, always relying on guard clauses to do all of our error checking up front, and ensuring the state is what we allow it to be at each point in time.
484
+
Consider breaking what would otherwise inevitably be a single large function into smaller functions. These may be organized by splitting functionality by steps, by different aspects of the same task, always relying on guard clauses to do all of our error checking up front, ensuring that state is constrained by what we allow it to be at each point in time.
485
485
486
+
The overall structure of your typical function should begin with guard clauses, making sure the input we receive is what we expect: enforcing required parameters, their correct data types, correct data ranges, and so on. If these inputs are malformed, we should bail immediately, ensuring we don't work with inputs we're unprepared to deal with, and ensuring the consumer gets an error message that explains the root reason why they're not getting the results they expect, as opposed to a message that might involve debugging work, such as `undefined is not a function` caused by trying to call an input that was supposed to be a function but wasn't -- or was supposed to result in our routine finding a function, but didn't.
486
487
488
+
Once we know the inputs are well-formed, data processing can begin. We'll transform the inputs, map them to the output we want to produce, and return that output. Here we have the opportunity to break apart the function into several pieces. Each aspect of the transformation of inputs into output is potentially its own function. The way of reducing complexity in a function is not by collapsing hundreds of lines of code into tens of complicated lines of code. Instead, we can move each of these long pieces of code into individual functions that only deal with one aspect of the data. Those functions can then also be hoisted out of our function and onto its parent scope, showing that there wasn't a reason why a particular aspect of transforming the inputs had to be coupled to the entire function doing the transformation.
487
489
490
+
Each aspect of a transformation operation can be analyzed and moved into its own function. The smaller function may take a few of the inputs in the larger function, or perhaps some of the intermediate values that were produced in the larger function. It can then conduct its own input sanitization, and be broken apart even further. The process of identifying aspects of an operation that can be recursively compartimentalized and moved into their own functions is highly effective because it allows for dauntingly large functions to be broken into simpler pieces that aren't as daunting to refactor.
491
+
492
+
At first, we can identify the largest aspects of a function, which might be 3 or 4, and break those apart. The first part might involve filtering out the parts of the input we're not interested in, the second might involve mapping that into something else, and the third part might involve merging all of the data together. Once we've identified each aspect of the function we might break those into their own functions, with their own inputs and output. Subsequently, we can do this for each of those smaller functions.
493
+
494
+
We can keep doing this for as long as there's opportunity for the functions to be simplified. As discussed in the previous section, it's valuable to take a step back after each of these refactors, and evaluate whether the end result is indeed simpler and easier to work with than what we had before it was refactored.
488
495
489
496
=== 4.3 State as Entropy
490
497
491
-
..
498
+
Entropy can be defined as a lack of order or predictability. The more entropy there is in a system, the more disordered and unpredictable the system becomes. Program state is a lot like entropy. Whether we're discussing global application state, user session state, or a particular component instance's state for a given user session, each bit of state we introduce to an application creates a new dimension to take into account when trying to understand the flow of a program, how it came to the state it's currently at, or how the current state dictates and helps predict the flow moving forward.
499
+
500
+
In this section, we'll discuss ways of eliminating and containing state, as well as immutability. First off, let's discuss what constitutes current state.
492
501
493
502
==== 4.3.1 Current State: It's Complicated
494
503
495
-
section on how state is terrible and the more state there is the less predictable our code becomes.
504
+
505
+
506
+
507
+
508
+
.. section on how state is terrible and the more state there is the less predictable our code becomes.
509
+
510
+
511
+
512
+
513
+
514
+
515
+
516
+
517
+
496
518
497
519
==== 4.3.2 Eliminating Incidental State
498
520
@@ -522,16 +544,12 @@ section on how state is terrible and the more state there is the less predictabl
522
544
523
545
.. How the right data structures can make our code much easier to write and read. It might be worth mapping data into something that's amenable to the task at hand, so that the algorithm is simplified by making the data easier to consume.
524
546
525
-
==== 4.4.4 Reducing, Mapping, Filtering, and Sorting Data
547
+
==== 4.4.4 Mapping, Filtering, Sorting, and Reducing Data
526
548
527
549
..
528
550
529
551
530
552
531
553
532
-
---
533
554
534
-
most of this chapter should be javascript code programming best practices:
535
-
look up code and figure out things you did. compare to code other people wrote and identify problems, explain.
0 commit comments