Skip to content

Commit bbc60a7

Browse files
authored
Merge branch 'master' into fix-uishell-skiptocontent
2 parents 5477873 + 5402407 commit bbc60a7

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

docs/style.md

+66
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ row before the </tbody></table> line.
6060
## Table of Contents
6161

6262
- [Introduction](#introduction)
63+
- [JavaScript](#javascript)
64+
- [Style](#style)
65+
- [Be explicit](#be-explicit)
6366
- [React](#react)
6467
- [Guidelines](#guidelines)
6568
- [Writing a component](#writing-a-component)
@@ -90,6 +93,69 @@ code:
9093
_Inspired by
9194
[Minimal API Surface Area](https://www.youtube.com/watch?v=4anAwXYqLG8)_
9295

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+
93159
## React
94160

95161
### Guidelines

0 commit comments

Comments
 (0)