From d782636d4753e88a66dd9ebd37255eea8c5b87d2 Mon Sep 17 00:00:00 2001 From: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Date: Wed, 10 Aug 2022 08:59:09 +0200 Subject: [PATCH] docs(best-practices): improve functions section (#17070) --- docs/development/best-practices.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/development/best-practices.md b/docs/development/best-practices.md index b7ae1911a5..1a0f30c021 100644 --- a/docs/development/best-practices.md +++ b/docs/development/best-practices.md @@ -21,20 +21,20 @@ Follow these best practices when you're working on our code. ### 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`. +- Use function declaration instead of assigning function expression into local variables (`const f = function(){...}`) (TypeScript already prevents rebinding functions) + - Exception: if the function accesses the outer scope's `this` then use arrow functions assigned to variables instead of function declarations - 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 +- Only use nested functions when the [lexical scope](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) is used #### Use arrow functions in expressions -Avoid +Avoid: ```ts bar(function(){...}) ``` -Use +Use: ```ts bar(() => { @@ -42,9 +42,10 @@ bar(() => { }); ``` -Function expressions may only be used if dynamically rebinding `this` is needed (Generally `this` pointer _should not_ be rebound). +Generally `this` pointer _should not_ be rebound. +Function expressions may only be used if dynamically rebinding `this` is needed. -[[Source](https://google.github.io/styleguide/tsguide.html#function-declarations)] +Source: [Google TypeScript Style Guide, function declarations](https://google.github.io/styleguide/tsguide.html#function-declarations). ## Code simplicity -- GitLab