Skip to content

Commit

Permalink
docs: added functions section to best-practices.md (#17065)
Browse files Browse the repository at this point in the history
added functions section to the best-practices.md
  • Loading branch information
Gabriel-Ladzaretti committed Aug 9, 2022
1 parent 99de3b7 commit 9bf0118
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/development/best-practices.md
Expand Up @@ -18,6 +18,34 @@ Follow these best practices when you're working on our code.
// istanbul ignore next: can never happen
```

### Functions

- Use `function foo(){...}` to declare named functions
- Use function declaration instead of assigning function expression into local variables (`const f = function(){...}`) (Typescript already prevents rebinding functions)
- Exception: Use arrow functions assigned to variables instead of function declarations if the function accesses the outer scope's `this`.
- Regular functions (as opposed to arrow functions and methods) _should not_ access `this`
- Use nested functions only when the [lexical scope](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) is utilized

#### Use arrow functions in expressions

Avoid

```ts
bar(function(){...})
```

Use

```ts
bar(() => {
this.doSomething();
});
```

Function expressions may only be used if dynamically rebinding `this` is needed (Generally `this` pointer _should not_ be rebound).

[[Source](https://google.github.io/styleguide/tsguide.html#function-declarations)]

## Code simplicity

### Write simple code
Expand Down

0 comments on commit 9bf0118

Please sign in to comment.