Conversation
- Copy the existing guide to style-guide-es5 - Add few ES6-specific sections - Update all code examples to use the new ES6 syntax
pages/en/contrib/style-guide.md
Outdated
|
|
||
| if (customer.isPreferred) { | ||
| discount = 0.1; | ||
| } else if (customer.address.state = 'CA') { |
pages/en/contrib/style-guide.md
Outdated
| return 0.1; | ||
| } | ||
|
|
||
| if (customer.address.state = 'CA') { |
pages/en/contrib/style-guide.md
Outdated
| arrow function or a regular function: | ||
|
|
||
| - Arrow function preserves `this` from the outer scope. This is much more | ||
| perfomant than the usual workaround of storing `this` in a `self` variable. |
There was a problem hiding this comment.
Typo: performant. I personally hate that word though. :-)
pages/en/contrib/style-guide.md
Outdated
|
|
||
| ### Classes | ||
|
|
||
| ES6 introduced syntax-sugar for defining classes, start using its' advantages |
pages/en/contrib/style-guide.md
Outdated
| {% include important.html content=" | ||
|
|
||
| The rules described in this document are evolving, therefore not all of our | ||
| code-base is following them yet. This is expected and OK. Our approach is to |
pages/en/contrib/style-guide.md
Outdated
|
|
||
| ### Variable declarations | ||
|
|
||
| Prefer to use immutable variables declared with `const` keyword: |
There was a problem hiding this comment.
Might be worth pointing out that const makes the reference immutable but not the referenced object. It's something that seems to trip up a lot of people.
pages/en/contrib/style-guide.md
Outdated
|
|
||
| ```js | ||
| const user = app.models.User; | ||
| const saltWorkFactor = user.settings.saltWorkFactor; |
There was a problem hiding this comment.
Arguably even better: const { saltWorkFactor } = user.settings;
Aside: I believe user should be User?
| ``` | ||
|
|
||
| In the rare cases where you need to split variable declaration and | ||
| initialization, prefer to use `let` over `var`: |
There was a problem hiding this comment.
let currently has a small caveat in that using it in loop constructs causes V8 to deoptimize the function, i.e., for now you should write:
for (var i = 0; i < 42; ++i) doit(i);Instead of:
for (let i = 0; i < 42; ++i) doit(i);This will be fixed in node.js v8.x but probably won't be fixed in v4.x and v6.x.
pages/en/contrib/style-guide.md
Outdated
|
|
||
| ### Arrow functions | ||
|
|
||
| The are two considerations to keep in mind when deciding whether to use an |
pages/en/contrib/style-guide.md
Outdated
| before/after hooks, then you cannot use an arrow function. | ||
|
|
||
| - Arrow functions are anonymous, there is no easy way for assigning them a | ||
| descriptive name. This makes it difficult to understand stack traces. |
There was a problem hiding this comment.
Half-true. V8 does a fairly good job of inferring a name when you assign them to variables.
b85c1ab to
0106dea
Compare
|
@bnoordhuis lovely ❤️ , thank you for your feedback! I applied all your comments in 0106dea, would you like to take another look? |
|
Would you also like me to add this to the "Contributing to LB" navigation - http://loopback.io/doc/en/contrib/code-contrib.html ? Easy enough to do - just LMK. |
|
Great feedback, thanks @bnoordhuis and @bajtos for updating. LGTM. |
The "code-contrib.html" already contains a link to "style-guide.md" AFAICT, see the bullet point "Adhere to code style outlined in the style guide.". (I updated an existing file in this patch, so that's expected.) Perhaps you are asking whether to add "style-guide-es5.md" to that document? I think that's not necessary, since most back-porting work is done by us, not the community. @superkhau thoughts? |
| // ... | ||
| } | ||
|
|
||
| set(modelName, key, value, options, callback) { |
I agree, we don't want more burden of maintaining ES5 docs in addition to ES6. That said though, is there a way to "freeze" the old ES5 docs with LB2 @crandmck? We won't maintain the old one, but you can still reference it up to this point. We will only maintain the latest docs and not backport to the old docs. |
Oh, I see, the new document replaces the old one, and you're saying that the community won't generally need the old (ES5) one. OK.
AFAIK, the only way to freeze it is to not accept any PRs to change it. We could also add a warning at the top of the page to say that it's specific to 2.x, and is "frozen", etc. Always need to consider that someone can land on the page (without context) from a search. |
I meant make a second copy of the .md file and add it to loopback.io as historic docs. You can git checkout to the point in time before the updates happened and copy the file directly. |
|
Maybe add a footnote somewhere in the new doc that points to the old docs? Ie. If you want to know the ES5 standard we followed before and not maintaining now, see link-here-to-copied-md-file. @crandmck Thoughts? |
What's the point of that? It's already at http://loopback.io/doc/en/contrib/style-guide-es5.html. The source file will of course live in GH as well.
Already there in the "See also" box at the top-right of http://loopback.io/doc/en/contrib/style-guide.html. |
You're right, there is no point when I think about it more clearly now. The correct approach is probably to link directly to the commit before the new changes.
Thoughts? |
|
@superkhau The current state of things is deliberate. There is a current style guide in
We already have that now.
Ditto, we have this too. I guess I don't understand what are the benefits of your proposal, compared to the current state? Also, is this discussion worth our time? |
|
@bajtos You're right, I just took a closer look at loopback.io. It already looks correct the way it is -- ES6 style guide promoted and ES5 doc in a referenceable link, both updateable if needs be.
I was just asking if there was a way to "freeze"/keep a copy of the ES5 docs around so we can have both available and backport if necessary. Since we're already there, this conversation is done. |
|
Is it available now? I can't find anything with ES6 in Loopback 3 documentation 😞 |
This pull request is adding/modifying the style guide for people contributing changes to Loopback, it's not primarily aimed at people writing applications using LoopBack as the framework. The content of this guide is available here: http://loopback.io/doc/en/contrib/style-guide.html |
Now that we have dropped support for pre-v4 versions of Node, it's time to start using ES6 goodness in our code base. In this patch, I did:
@superkhau @strongloop/loopback-dev PTAL
@sam-github @bnoordhuis Since you are contributing to Node core, which has switched to ES6 long time ago, I think you can help us with avoiding mistakes others have already learnt from. Are any parts of my proposal different from current best practices in Node core? Is there any other ES6-related best practices to add to our style guide?