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/ch05.asciidoc
+49-6Lines changed: 49 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,64 @@
1
1
[[modular-patterns-and-practices]]
2
2
== Modular Patterns and Practices
3
3
4
-
..
4
+
In this chapter we'll take a look at some of the latest language features and how we can leverage those in our programs while reducing complexity in the process. We'll also analyze concrete coding patterns and conventions that can help us develop simple alternatives to otherwise complex problems.
5
5
6
6
=== 5.1 Leveraging Modern JavaScript
7
7
8
-
# discuss role of latest language features, spread, how they can make code more maintainable, async/await, promises, etc
8
+
When used conscientiously, the latest JavaScript features can be of great help in reducing the amount of code whose sole purpose is to work around language limitations, increasing signal -- the amount of valuable information that can be extracted from reading a piece of code -- while eliminating boilerplate and repetition.
9
+
10
+
==== 5.1.1 Template Literals
11
+
12
+
Before ES6, the JavaScript community came up with half a dozen ways of arriving at multi-line strings: from strings chained with `\` escape characters or `+` arithmetic operators, to using `Array#join`, or resorting to the string representation of comments in a function -- all merely for multi-line support.
13
+
14
+
Further, inserting variables into a string isn't possible, but that's easily circumvented by concatenating them with one or more strings.
15
+
16
+
[source,javascript]
17
+
----
18
+
'Hello ' + name + ', I\'m Nicolás!'
19
+
----
20
+
21
+
Template literals arrived in ES6 and resolve multi-line strings in a feature that was native to the language, without the need for any clever hacks in user-space.
22
+
23
+
Unlike strings, with template literals we can interpolate expressions using a streamlined syntax. They involve less escaping, too, thanks to using backticks instead of single or double quotation marks, which appear more frequently in English text.
24
+
25
+
[source,javascript]
26
+
----
27
+
`Hello ${ name }, I'm Nicolás!`
28
+
----
29
+
30
+
Besides these improvements, template literals also offer the possibility of tagged templates. You can prefix the template with a custom function that transforms the template's output use cases like input sanitization, formatting, or anything else.
31
+
32
+
Whenever we need to compose a string using data, template literals are a terse alternative to string concatenation. When we want to avoid escaping single or double quotes, template literals can help. The same is true when we want to write multi-line strings.
33
+
34
+
In every other case -- when there's no interpolation, escaping, or multi-line needs -- the choice comes down to a mere matter of style. In the last chapter of Practical Modern JavaScript, "Practical Considerations", I advocatedfootnoteref:[template-literals,You can read a blog post I wrote about why template literals are better than strings at: https://mjavascript.com/out/template-literals. Practical Modern JavaScript (O'Reilly, 2017) is the first book in the Modular JavaScript series. You're currently reading the second book of the same series.] in favor of using template literals in every case. This was for a few factors, but here's the two most important ones: because of convenience, so that you don't have to convert a string back and forth between single quoted string and template literals depending on its contents; and because of consistency, so that you don't have to stop and think about which kind of quotation mark -- single, double, or backtick -- to use each time. Template literals may take some time to get accustomed to: we've used single quoted strings for a long time, and template literals have only been around for a while. You or your team might prefer sticking with single quoted strings, and that's perfectly fine too.
35
+
36
+
[NOTE]
37
+
====
38
+
When it comes to style choices, you'll rarely face problems if you let your team come to a consensus about the preferred style choice and later enforce that choice by way of a lint tool like ESLint. It's entirely valid ot stick with single quoted strings and only use template literals when deemed absolutely necessary, if that's what most of the team prefers.
39
+
40
+
Using a tool like ESLint and a continuous integration job to enforce its rules means nobody has to perform the time consuming job of keeping everyone in line with the house style. When tooling enforces style choices, discussions about those choices won't crop up as often in discussion threads while contributors are collaborating on units of work.
41
+
====
42
+
43
+
It's important to differentiate between purely stylistic choices, which tend to devolve in contentious time-sinking discussions, and choices where there's more ground to be covered in the everlasting battle against complexity.
44
+
45
+
==== 5.1.2 Destructuring, Rest, and Spread
46
+
47
+
9
48
10
-
==== 5.1.1 Destructuring, Rest, Spread, and Template Literals
11
49
12
50
.. how each of these features helps us keep code simpler, or how to simplify existing code using them.
13
51
14
-
==== 5.1.2 Striving for simple `const` bindings
52
+
53
+
54
+
55
+
56
+
57
+
==== 5.1.3 Striving for simple `const` bindings
15
58
16
59
.. thinking about how `let` and `var` may mean we're doing complicated things, and how striving to avoid those bindings almost always leads to better/simpler code.
17
60
18
-
==== 5.1.3 Asynchronous Functions and Promises
61
+
==== 5.1.4 Asynchronous Functions and Promises
19
62
20
63
.. why converting flows to promises may be a good way of simplifying it, on the basis that a lot of the language now is built around support for promises
21
64
@@ -35,7 +78,7 @@ how we can decorate components either via composition or decorators to add featu
35
78
36
79
in real apps you'll seldom have to use inheritance except when connecting to specific frameworks you depend on, or for specific patterns, everywhere else probably use composition [ellaborate on that]
0 commit comments