Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: fix nits in code examples of async_hooks.md #13400

Closed
wants to merge 6 commits into from
Closed

doc: fix nits in code examples of async_hooks.md #13400

wants to merge 6 commits into from

Conversation

vsemozhetbyt
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt commented Jun 2, 2017

Checklist
Affected core subsystem(s)

doc, async_hooks

  • Make require() consistent.
  • Add missing argument.
  • Add missing \n in outputs.
  • Reduce string concatenations.
  • Update outputs.
  • Reword and fix a typo.

@nodejs-github-bot nodejs-github-bot added async_hooks Issues and PRs related to the async hooks subsystem. doc Issues and PRs related to the documentations. labels Jun 2, 2017
@vsemozhetbyt
Copy link
Contributor Author

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jun 2, 2017

I am unsure about 2 fragments if there are typos there:

  1. https://github.com/nodejs/node/blame/126170a05fc044c88fe0f27fc81fcdb9e75d5a0b/doc/api/async_hooks.md#L249-L251

    ...so triggerId is given the task of propagating...

    Should it be 'stack of propagating'?

  2. https://github.com/nodejs/node/blame/126170a05fc044c88fe0f27fc81fcdb9e75d5a0b/doc/api/async_hooks.md#L430-L432

    // Though the resource that caused (or triggered) this callback to
    // be called was that of the new connection. Thus the return value
    // of triggerId() is the ID of "conn".

    Should the 'Though' be removed or '. Thus' be replaced by comma or nothing?

Let me know if these are to be addressed too.

fs.open(path, (err, fd) => {
console.log(async_hooks.currentId()); // 2 - open()
fs.open(path, 'r', (err, fd) => {
console.log(async_hooks.currentId()); // 6 - open()
Copy link
Member

@AndreasMadsen AndreasMadsen Jun 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something is wrong with the async_hooks.currentId() this is supposed to be 2.

edit: oh, it might be because of console.log. Then 6 is likely correct.


let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerId) {
const cId = async_hooks.currentId();
fs.writeSync(1, ' '.repeat(indent) + `${type}(${asyncId}): ` +
fs.writeSync(1, `${' '.repeat(indent)}${type}(${asyncId}): ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care. But for me, this is not more readable than the original.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, for me, mixing template literals with string concatenation seems a bit confusing. However, if there are some -1, I can revert.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW You know I like it when you assign expressions to consts, make it "less operations per line"
But if not I'm +1 for homogeneous templates vs. templates + contacts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now I see it's used for all hooks, so +4 on const indentString = ' '.repeat(indent)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @AndreasMadsen , the previous version was more readable in my eyes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the lack of separation between formatting (' '.repeat(indent)) and information (${type}(${asyncId}). Maybe if ${indentString} ${type}(${asyncId}): is used we get the best of both worlds.

@AndreasMadsen
Copy link
Member

Should it be 'stack of propagating'?

No, task it correct. Although "purpose" might be a better word.

Should the 'Though' be removed or '. Thus' be replaced by comma or nothing?

Yes, those are good suggestions.

@vsemozhetbyt
Copy link
Contributor Author

New linter CI after rewording and fixing a typo: https://ci.nodejs.org/job/node-test-linter/9553/

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits

@@ -221,12 +221,13 @@ The following is a simple demonstration of `triggerId`:

```js
const async_hooks = require('async_hooks');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say remove both, snippets don't have to be complete, so this is cruft.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, they need not, but many readers will test the snippets, so I prefer to be consistent towards completeness, not implying (RE: empathy :)

@@ -272,24 +273,25 @@ elaborate to make calling context easier to see.

```js
const async_hooks = require('async_hooks');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerId) {
const cId = async_hooks.currentId();
fs.writeSync(1, ' '.repeat(indent) + `${type}(${asyncId}): ` +
fs.writeSync(1, `${' '.repeat(indent)}${type}(${asyncId}): ` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW You know I like it when you assign expressions to consts, make it "less operations per line"
But if not I'm +1 for homogeneous templates vs. templates + contacts


let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerId) {
const cId = async_hooks.currentId();
fs.writeSync(1, ' '.repeat(indent) + `${type}(${asyncId}): ` +
fs.writeSync(1, `${' '.repeat(indent)}${type}(${asyncId}): ` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now I see it's used for all hooks, so +4 on const indentString = ' '.repeat(indent)

// of triggerId() is the ID of "conn".
// The resource that caused (or triggered) this callback to be called
// was that of the new connection. Thus the return value of triggerId()
// is the ID of "conn".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndreasMadsen /s/ID/asyncId/ ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@@ -462,6 +464,8 @@ will occur and node will abort.
The following is an overview of the `AsyncResource` API.

```js
const { AsyncResource } = require('async_hooks');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

less (because of the destructuring) but still cruft

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place where a reader can find out how to obtain AsyncResource.

@refack
Copy link
Contributor

refack commented Jun 4, 2017

cruft - https://en.wikipedia.org/wiki/Cruft


https://github.com/nodejs/node/blame/126170a05fc044c88fe0f27fc81fcdb9e75d5a0b/doc/api/async_hooks.md#L249-L251

...so triggerId is given the task of propagating...

That sentence is too metaphorical (borderline poetry 😉). I would suggest:

With only that information it would be impossible to link resources together,
so `triggerId` is provided as a reference to which resource caused the creation of the new resource.

@refack
Copy link
Contributor

refack commented Jun 4, 2017

About the requires, just food for thought (I'm not opposing)

  1. most other api docs don't put those in all snippets - https://github.com/nodejs/node/blob/master/doc/api/fs.md#fsappendfilefile-data-options-callback
  2. there's empathy and there's spoon-feeding (and cruft). IMHO the API docs are more for reference then a tutorial so I do assume some knowledge. I Personally use the docs several times a day, so I like them complete but succinct.

@refack
Copy link
Contributor

refack commented Jun 4, 2017

One more thing. I know a guy who is a big contributor in StackExchange, he always puts tiny mistakes in his snippets so the users will have to understand them in order to fix the problem.
I'm not suggesting we do that, but we aim to explain (maybe teach) not just enable copy&paste culture.

@addaleax
Copy link
Member

addaleax commented Jun 4, 2017

@refack One nice thing about explicit requires is that we’re moving closer to having the examples runnable as stand-alone scripts, so we could use them as tests (just to check that our doc examples work, I’ve done this for private projects and it’s worth it) and maybe enable a “run this script in the browser” functionality, some of us – I don’t recall who – have talked about that a few times with some of the providers that do that kind of thing.

IMHO the API docs are more for reference then a tutorial so I do assume some knowledge. I Personally use the docs several times a day, so I like them complete but succinct.

One problem that we might not notice much is that much of the Node documentation is written by people with an extensive understanding of how Node works, like everybody in this conversation. I can’t actually tell whether those extra bits of code are helpful, because how to create them is obvious to me even in the case of the destructuring ones, and I suspect the same might be true for you. ;)

@vsemozhetbyt
Copy link
Contributor Author

I've tried to address comments:

Commit 1: remove 2 require() for consistency, add 1 (otherwise a reader cannot find out how to get the class constructor without reading the lib code).

Commit 4: add a helper variable to reduce clutter.

Commit 6: ID -> asyncId.

@vsemozhetbyt
Copy link
Contributor Author

@refack
Copy link
Contributor

refack commented Jun 5, 2017

@refack One nice thing about explicit requires is that we’re moving closer to having the examples runnable as stand-alone scripts, so we could use them as tests

That's a very laudable goal!

One problem that we might not notice much is that much of the Node documentation is written by people with an extensive understanding of how Node works, like everybody in this conversation. I can’t actually tell whether those extra bits of code are helpful, because how to create them is obvious to me even in the case of the destructuring ones, and I suspect the same might be true for you. ;)

Definitely! I agree it's hard to find the right balance.
After all the API docs are a product, and from my experience it's almost impossible to design a good product without either being part of it's audience or getting constant feedback from the market. I wish users would open more issues for the docs.

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good balance 💯

@vsemozhetbyt
Copy link
Contributor Author

Landed in 5d9dc94

@vsemozhetbyt vsemozhetbyt deleted the async_hooks.md branch June 5, 2017 13:49
vsemozhetbyt added a commit that referenced this pull request Jun 5, 2017
* Make `require()` consistent.
* Add missing argument.
* Add missing `\n` in outputs.
* Reduce string concatenations.
* Update outputs.
* Reword and fix a typo.

PR-URL: #13400
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
jasnell pushed a commit that referenced this pull request Jun 5, 2017
* Make `require()` consistent.
* Add missing argument.
* Add missing `\n` in outputs.
* Reduce string concatenations.
* Update outputs.
* Reword and fix a typo.

PR-URL: #13400
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
@jasnell jasnell mentioned this pull request Jun 5, 2017
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants