@@ -60,6 +60,9 @@ row before the </tbody></table> line.
60
60
## Table of Contents
61
61
62
62
- [ Introduction] ( #introduction )
63
+ - [ JavaScript] ( #javascript )
64
+ - [ Style] ( #style )
65
+ - [ Be explicit] ( #be-explicit )
63
66
- [ React] ( #react )
64
67
- [ Guidelines] ( #guidelines )
65
68
- [ Writing a component] ( #writing-a-component )
90
93
_ Inspired by
91
94
[ Minimal API Surface Area] ( https://www.youtube.com/watch?v=4anAwXYqLG8 ) _
92
95
96
+ ## JavaScript
97
+
98
+ ### Style
99
+
100
+ #### Be explicit
101
+
102
+ <table >
103
+ <thead ><tr ><th >Unpreferred</th ><th >Preferred</th ></tr ></thead >
104
+ <tbody >
105
+ <tr ><td >
106
+
107
+ ``` jsx
108
+ const add = (a , b ) => a + b;
109
+ ```
110
+
111
+ </td ><td >
112
+
113
+ ``` jsx
114
+ const add = (a , b ) => {
115
+ return a + b;
116
+ };
117
+ ```
118
+
119
+ </td ></tr >
120
+ </tbody ></table >
121
+
122
+ Certain features in JavaScript have implicit behavior. One of these that we see
123
+ most often is the implicit return behavior of arrow function expressions, for
124
+ example:
125
+
126
+ ``` js
127
+ const add = (a , b ) => a + b;
128
+ ```
129
+
130
+ We've found that, while this style is terse and compact, it can be at odds with
131
+ the fact that code is revisited often and that developers need to peak inside
132
+ sometimes to see what is going on. For example, if we needed to debug a specific
133
+ value in the function above then we would go through the following steps:
134
+
135
+ ``` js
136
+ // Step 1. The code as originally authored
137
+ const add = (a , b ) => a + b;
138
+
139
+ // Step 2. Update the code to no longer use the implicit return
140
+ const add = (a , b ) => {
141
+ return a + b;
142
+ };
143
+
144
+ // Step 3. Add any debugging code or ways to introspect its values
145
+ const add = (a , b ) => {
146
+ console .log (a);
147
+ return a + b;
148
+ };
149
+
150
+ // Step 4. Undo these changes and bring back to original format
151
+ const add = (a , b ) => a + b;
152
+ ```
153
+
154
+ If instead we had written this code without the implicit return then we would
155
+ have saved three out of the four steps above. As a result, we tend to favor
156
+ being explicit in how JavaScript is written instead of relying on implicit
157
+ behavior.
158
+
93
159
## React
94
160
95
161
### Guidelines
0 commit comments