Skip to content

Commit

Permalink
Add a few dev guide based on observations (#6449)
Browse files Browse the repository at this point in the history
* add a few developer guides based on observations

Signed-off-by: Lu Yu <[email protected]>

* add more info

Signed-off-by: Lu Yu <[email protected]>

---------

Signed-off-by: Lu Yu <[email protected]>
(cherry picked from commit 3d91b94)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Apr 16, 2024
1 parent e33cc22 commit 8a25de1
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,42 @@ if (width < 300) {
}
```
#### Avoid using `var` to declare variables
Use `const` by default, and never use `var` to declare variables. `const` and `let` are block scoped, like variables in most other languages. `var` in JavaScript is function scoped, which can cause difficult to understand bugs.

#### Avoid using the `Array` constructor
Do not use the Array() constructor, with or without new. It has confusing and contradictory usage.

Instead, always use bracket notation to initialize arrays.

```js
// good
const arr = [2];

// bad
const arr = new Array(2); //[undefined, undefined]
const arr = new Array(2, 3); //[2, 3];
```

#### Avoid line continuations in string literals
Do not use line continuations (that is, ending a line inside a string literal with a backslash) in either ordinary or template string literals. Even though ES5 allows this, it can lead to tricky errors if any trailing whitespace comes after the slash, and is less obvious to readers.

```js
// good
const LONG_STRING = 'This is a very very very very very very long string. ' +
'It does not contain long stretches of spaces because it uses ' +
'concatenated strings.';

// bad
const LONG_STRING = 'This is a very very very very very very very long string. \
It inadvertently contains long stretches of spaces due to how the \
continued lines are indented.';
```

#### Avoid using `@ts-ignore`
Do not use @ts-ignore nor the variants @ts-expect-error or @ts-nocheck. They superficially seem to be an easy way to fix a compiler error, but in practice, a specific compiler error is often caused by a larger problem that can be fixed more directly.


#### Use native ES2015 module syntax

Module dependencies should be written using native ES2015 syntax wherever
Expand Down Expand Up @@ -936,6 +972,31 @@ Do not use setters, they cause more problems than they can solve.
[sideeffect]: http://en.wikipedia.org/wiki/Side_effect_(computer_science)
#### Use strict equality checks
Use strict equality operators (===/!==) to compare the operands. The equality (==/!=) operator will try to convert and compare operands that are of different types causing unexpected behavior.
#### Use uppercase for constants
Constants should be declared in uppercase letters especially for primitives because they are truly immutable.
#### Use named exports
Use named exports instead of default exports. Default exports provide no canonical name, which makes central maintenance difficult with relatively little benefit to code owners, including potentially decreased readability.
```js
// good
export class User { ... }

// bad
export default class User { ... }
// why bad
import User from './user'; // Legal.
import Group from './user'; // Also legal.
```
#### Use single quotes for string literals
Ordinary string literals are delimited with single quotes ('), rather than double quotes ("). If a string contains a single quote character, consider using a template string to avoid having to escape the quote.
#### Attribution
Parts of the JavaScript developer guide were initially forked from the
Expand Down

0 comments on commit 8a25de1

Please sign in to comment.