Acronyms and initialisms
that commonly appear as all upper case in American English should be uniformly
up- or down-cased according to case conventions:
var utf8Bytes: [UTF8.CodeUnit]
var isRepresentableAsASCII = true
var userSMTPServer: SecureSMTPServer
I find camelCase easier to read when the acronym isn’t the last word,
particularly when one acronym follows another.
JavaScript / TypeScript
Prettier
Configurations I prefer over the defaults:
prettier.config.js
/** @type{import('prettier').Config} */
exportdefault {
singleQuote: true,
semi: false,
bracketSameLine: true,
}
These are mostly personal preferences, but also to avoid “noise and chores”,
or as Kent C. Dodds
puts it in his words ,
to avoid “babysitting”.
let v.s. const
I used to prefer-const
for many years, like most of the others. However, after reading
const-antly Confused
from the book You Don’t Know JS Yet ,
I reflected on my own code and realized that it has little value for me,
but sometimes add noises:
Cause Prettier to break lines when let could fit the code in one line.
Need to change const to let when needed to reassign later.
Especially annoying when the variable is in destructuring.
So I now use let by default and only use const for true “constants”.
After reading Type Aware Equality ,
I realized that in most cases, x === y logically equals to
typeof x == typeof y && x == y. Interestingly,
eqeqeq has a “smart” option ,
in which you can use x == y when it is clear whether typeof x == typeof y.
When use TypeScript, it is clear most of the time.
So the extra = in === is mostly noise in TypeScript code except for a few cases.
One of the cases is type unions, as in let x: string | number | boolean.
I may consider using === for early returns, for instance, if (x === false) return,
However, type checks like if (typeof x == 'boolean')... are more preferred.
Another (edge) case is [] == ![]. I am not sure if I will ever encounter it.
Will update if I ever do.
Named exports v.s. Default exports
For a JavaScript module, exports are public APIs,
for which I prioritize Clarity at the Call Site.
So whether to use named or default exports depends on how the module is used.
When the export is imported on the call site and used in code with a name,
the name should be part of the API, Therefore, named exports should be used.
When a third-party tool loads the module,
instead of it being imported and used in code,
a default export should be used.
In most cases, it is the sole purpose of the entire module.