[comment]: # "markdown: { smartypants: true }" ## Go Proverbs (JavaScript Edition!)
## ~~Go~~ Node Proverbs
AJ ONeal
[@\_beyondcode](https://twitter.com/@_beyondcode)
[twitch.tv/coolaj86](https://twitch.tv/coolaj86)
Dangerous Wrong Thinker Equal Opportunity Offender Technophobic Technologist Extraordinairé
Creeds of Craftsmanship > The most important archaeological finds of the 22nd > century.
- The Zen of Python - The Go Proverbs - ... (more to come)
Go Proverbs with Rob Pike
https://youtube.com/watch?v=PAAkCSZUG1c&t=0s
Creeds of Craftsmanship . com
1\) Don't communicate by sharing memory, share memory by communicating.
Don't communicate by sharing memory... ```js var done = false; if (done) { // ... } ```
Share memory by communicating ```js promise.then(...); ``` ```js $el.on('click', function () { ... }); ```
2\) Concurrency is not parallelism.
Concurrency ```js app.get("/api/hello", function (req, res) { // ... res.json({ success: true, }); }); ```
Parallelism ```js Promise.all(photos.map(Resizer.resize)); ```
3\) Channels orchestrate; mutexes serialize.
~~3\) Channels orchestrate; mutexes serialize.~~ 3\) Promises & Events orchestrate; await serializes.
Orchestration ```js await Promise.all([ Promise.race(getMilk(), getSoy()), Promise.race(getEggs(), getApplesauce()), getDishes(), getUtensils(), ]); ```
Serialization (Chaining) ```js await mix(); await bake(); await serve(); ```
4\) The bigger the interface, the weaker the abstraction.
Small interface ```js Database.engine = { get: async function (key) { let path = safeJoin(dir, key); return await fs.readFile(path, "utf8"); }, set: async function (key, text) { await fs.writeFile(key, text, "utf8"); }, }; ```
Big interface ```js Database.engine = { get: function (/* ... */) {}, set: function (/* ... */) {}, index: function (/* ... */) {}, remove: function (/* ... */) {}, findBy: function (/* ... */) {}, createCursor: function (/* ... */) {}, createTransaction: function (/* ... */) {}, }; ```
5\) Make the zero value useful.
Ugly: ```js const foo = hasBaz ? "baz" : "bar"; ```
Useful: ```js let foo = "bar"; if (hasBaz) { foo = "baz"; } ```
~~5\) Make the zero value useful.~~ 5b\) Make the unexpected value useful.
Dangerous ```js let isExpired = token.expires > Date.now(); if (isExpired) { // ... } ```
Safe ```js let isFresh = token.expires < Date.now(); if (!isFresh) { // ... } ```
6\) interface{} says nothing.
```js // TODO ```
7\) Gofmt's style is no one's favorite, yet `gofmt` is everyone's favorite.
~~7\) Gofmt's style is no one's favorite, yet `gofmt` is everyone's favorite.~~ 7\) Prettier's style is no one's favorite, yet `prettier` is everyone's favorite.
`package.json`: ```json { "scripts": { "fmt": "npx prettier@2 -w '**/*.{js,md}'" } } ``` ```js npm run fmt ```
[The Prettier Rationale](https://prettier.io/docs/en/rationale.html)
8\) A little copying is better than a little dependency.
## `leftPad`
## `lodash`
```js Object.keys(obj).forEach(function (key) { let val = obj[key]; // ... }); ```
9\) Syscall must always be guarded with build tags.
# 🤷♂️
10\) Cgo must always be guarded with build tags.
# 🤷♂️
11\) Cgo is not Go.
~~11\) Cgo is not Go.~~ - JSX is not JS. - ECMAScript is not JavaScript. - Babel is not ECMAScript.
12\) With the unsafe package there are no guarantees.
# 🤷♂️
13\) Clear is better than clever.
Clear: ```js let foo = "bar"; if (hasBaz) { foo = "baz"; } ```
Clever: ```js const foo = hasBaz ? "baz" : "bar"; ```
14\) Reflection is never clear.
~~14\) Reflection is never clear.~~ 14\) Meta programming is never clear.
```js thing.count += 1; ```
```js Object.defineProperty(thing, "count", { get: function () { return thing.count || 0; }, writable: false, }); ```
```js let thing = { count: 0, }; ```
15\) Errors are values.
Bad: ```js try { await doStuff(); } catch (e) { // ... } ```
Good: ```js doStuff().catch(function (err) { // handle or rethrow }); ```
16\) Don't just check errors, handle them gracefully.
Check: ```js doStuff().catch(function (err) { res.end("oops!"); }); ```
Handler:
```js doStuff().catch(function (err) { if (Db.noRecord === err) { return {}; } throw err; }); ```
17\) Design the architecture, name the components, document the details.
# 👍
18\) Documentation is for users.
# 📝
19\) Don't panic.
```js await doStuff().catch(...); ```