Skip to content

Commit 2991a32

Browse files
committed
revealing pattern
1 parent b88f211 commit 2991a32

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

chapters/ch05.asciidoc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,29 @@ As with most things programming, your codebase will benefit from a semblance of
344344

345345
=== 5.3 Code Patterns
346346

347-
..
347+
Digging a bit deeper and into more specific elements of architecture design, in this section we'll explore a few of the most common patterns for creating boundaries from which complexity cannot escape, encapsulating functionality, and communicating across these boundaries or application layers.
348348

349349
==== 5.3.1 Revealing Module
350350

351-
.. this one is sort-of obvious but explain how hiding most of a class in a function can help avoid unwarranted access
351+
The revealing module pattern has become a staple in the world of JavaScript. The premise is simple enough: expose precisely what consumers should be able to access, and avoid exposing anything else. The reasons for this are manifold. Preventing unwarranted access to implementation details reduces the likelihood of your module's interface being abused for unsupported use cases that might bring headaches to both the module implementer and the consumer alike.
352+
353+
Explicitly avoid exposing methods meant to be private, such as a hypothetical `_calculatePriceHistory` method, which relies on the leading hyphen as a way of discouraging direct access and signaling that it should be regarded as private. Avoiding such methods prevents test code from accessing private methods directly, resulting in tests that make assertions solely regarding the interface and which can be later referenced as documentation on how to use the interface; prevents consumers from monkey-patching implementation details, leading to more transparent interfaces; and also often results in cleaner interfaces due to the fact that the interface is all there is, and there's no alternative ways of interacting with the module through direct use of its internals.
354+
355+
JavaScript modules are of a revealing nature by default, making it easy for us to follow the revealing pattern of not giving away access to implementation details. Any function, object, class, or variable we declare is private unless we explicitly decide to `export` it from the module.
356+
357+
When we expose only a thin interface, our implementation can change largely without having an impact on how consumers use the module, nor on the tests that cover the module. As a mental exercise, always be on the lookout for aspects of an interface that should be turned into implementation details and extricated from the interface itself.
352358

353359
==== 5.3.2 Object Factories
354360

355361
.. and how they're great for keeping state contained to the instance
356362

363+
364+
365+
366+
367+
368+
369+
357370
==== 5.3.3 Event Emission
358371

359372
.. events for things that we want to have clearly-delineated side-effects.

0 commit comments

Comments
 (0)