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: cleanup and add references to CPP_STYLE_GUIDE.md #23650

Merged
merged 1 commit into from
Oct 16, 2018

Conversation

refack
Copy link
Contributor

@refack refack commented Oct 14, 2018

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Oct 14, 2018
@refack refack self-assigned this Oct 14, 2018
@refack refack added the c++ Issues and PRs that require attention from people who are familiar with C++. label Oct 14, 2018
not inside of nested calls.

Using C++ `throw` is not allowed.
Node.js is built without C++ exception semantics, so code using `thorw` or, even
Copy link
Member

Choose a reason for hiding this comment

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

thorw->throw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack

```cpp
std::unique_ptr<Foo> FooFactory();
void FooConsumer(std::unique_ptr<Foo> ptr);
```
Since `std::unique_ptr` has only move semantics, passing one by value trasfers
Copy link
Member

Choose a reason for hiding this comment

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

trasfers->transfers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack

@@ -316,13 +311,19 @@ exports.foo = function(str) {

#### Avoid throwing JavaScript errors in nested C++ methods

When you have to throw the errors from C++, try to do it at the top level and
When you need to return an error from C++, try to do it at the top level and
Copy link
Member

Choose a reason for hiding this comment

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

This referred specifically to throwing exceptions, though.

(s/error/exception/ would be a good change here, that’s on me.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you clear this up for me? we can't c++ throw, we need to isolate()->ThrowException() right?

- Use `static_cast` for casting whenever it works
- `reinterpret_cast` is okay if `static_cast` is not appropriate
- Use `static_cast<T>` if casting is required, and its valid
- Use `reinterpret_cast` only when its essential
Copy link
Member

Choose a reason for hiding this comment

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

typo: it is (twice)

(and maybe s/essential/necessary/)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack x 3


Never use `std::auto_ptr`. Instead, use `std::unique_ptr`.
Avoid `std::auto_ptr`. It was deprecated in C++11 and removed from C++17.
Copy link
Member

Choose a reason for hiding this comment

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

This seemed sensible in previous form? There’s no reason to use it in our code, and that it’s deprecated/removed is not really of concern if you already know not to use it

Copy link
Contributor Author

@refack refack Oct 14, 2018

Choose a reason for hiding this comment

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

I didn't like the phrasing "Never use", also I do believe an explanation why is nice, since it's very short.

Maybe:

Don't use `std::auto_ptr`.<br/>
It was deprecated in C++11 if favor of `std::unique_ptr`, and later removed from
C++17. `std::unique_ptr` has move semantics instead the of unusual copy
semantics of `std::auto_ptr`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually we should be able to lint for that. I'll see if I can figure out how to.

```cpp
std::unique_ptr<Foo> FooFactory();
void FooConsumer(std::unique_ptr<Foo> ptr);
```
Since `std::unique_ptr` has only move semantics, passing one by value trasfers
Copy link
Member

Choose a reason for hiding this comment

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

nit: blank lines around code block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack

@@ -192,40 +192,35 @@ class FancyContainer {

### Use `nullptr` instead of `NULL` or `0`

What it says in the title.
Further reading at [ES.47].
Copy link
Member

Choose a reason for hiding this comment

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

I’d really like it if we could make these links more expressive instead of just having the item numbers in the text, e.g. Further reading in the [C++ Core Guidelines][ES.47]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack


Prefer to use `std::unique_ptr` to make ownership
transfer explicit. For example:
[R.20]: Use `std::unique_ptr` or `std::shared_ptr` to represent ownership
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Oct 14, 2018

Choose a reason for hiding this comment

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

Currently, this and next lines are rendered as one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack

Prefer to use `std::unique_ptr` to make ownership
transfer explicit. For example:
[R.20]: Use `std::unique_ptr` or `std::shared_ptr` to represent ownership<br/>
[R.21]: Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership
Copy link
Member

Choose a reason for hiding this comment

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

If you want a list of things, you can juse start lines with * instead of manually inserting line breaks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack x 3

Since `std::unique_ptr` has only move semantics, passing one by value transfers
ownership to the callee and invalidates the instance the caller hold.

Don't use `std::auto_ptr`.<br/>
Copy link
Member

Choose a reason for hiding this comment

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

No need for a line break, this makes sense as one paragraph.


Further reading:<br/>
[ES.48]: Avoid casts<br/>
[ES.49]: If you must use a cast, use a named cast
Copy link
Member

Choose a reason for hiding this comment

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

ditto here

@refack
Copy link
Contributor Author

refack commented Oct 14, 2018

New format:
image

Explicit <br/> format:
image

🤔

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -316,13 +316,19 @@ exports.foo = function(str) {

#### Avoid throwing JavaScript errors in nested C++ methods

When you have to throw the errors from C++, try to do it at the top level and
When you need to throw an exception from C++, try to do it at the top level and
Copy link
Member

Choose a reason for hiding this comment

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

This somehow reads a bit ambiguous to me now that it's exception, can you change it to JavaScript exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack

Since `std::unique_ptr` has only move semantics, passing one by value transfers
ownership to the callee and invalidates the instance the caller hold.

Don't use `std::auto_ptr`. It was deprecated in C++11 if favor of
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to explain this here? Can we link to some docs somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack (ish). PTAL

When you need to throw a JavaScript exception from C++ (i.e.
`isolate()->ThrowException()`) prefer to do it as close to the return to JS as
possible, and not inside of nested C++ calls. Since this changes the `isolate`'s
state doing closest to where it is consumed, reduces the chance of side effects.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joyeecheung, @addaleax PTAL
I'm trying to grok this, so I wrote it down.

Copy link
Member

Choose a reason for hiding this comment

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

s/isolate/Isolate/, and the comma should be removed

I don’t think it matters that it’s an Isolate thing, though. That’s implementation details, and you can just say “JS execution state” or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack


Using C++ `throw` is not allowed.
Node.js is built without C++ exception semantics, so code using `throw` or, even
Copy link
Member

Choose a reason for hiding this comment

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

s/exception semantics/exceptions/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went with exception handling and added a reference (GCC state 7% runtime cost which I think is interesting).

When you need to throw a JavaScript exception from C++ (i.e.
`isolate()->ThrowException()`) prefer to do it as close to the return to JS as
possible, and not inside of nested C++ calls. Since this changes the `isolate`'s
state doing closest to where it is consumed, reduces the chance of side effects.
Copy link
Member

Choose a reason for hiding this comment

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

s/isolate/Isolate/, and the comma should be removed

I don’t think it matters that it’s an Isolate thing, though. That’s implementation details, and you can just say “JS execution state” or something like that.

@refack refack force-pushed the more-references-in-cpp-guide branch from 41872c5 to 5764fdb Compare October 16, 2018 15:32
@refack
Copy link
Contributor Author

refack commented Oct 16, 2018

@refack refack added the meta Issues and PRs related to the general management of the project. label Oct 16, 2018
PR-URL: nodejs#23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@refack refack force-pushed the more-references-in-cpp-guide branch from 5764fdb to cf3f8dd Compare October 16, 2018 21:50
@refack refack merged commit cf3f8dd into nodejs:master Oct 16, 2018
@refack refack deleted the more-references-in-cpp-guide branch October 16, 2018 22:05
jasnell pushed a commit that referenced this pull request Oct 17, 2018
PR-URL: #23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
addaleax pushed a commit that referenced this pull request Oct 20, 2018
PR-URL: #23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@refack refack removed their assignment Oct 20, 2018
MylesBorins pushed a commit that referenced this pull request Oct 30, 2018
PR-URL: #23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@codebytere codebytere mentioned this pull request Nov 27, 2018
rvagg pushed a commit that referenced this pull request Nov 28, 2018
PR-URL: #23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
MylesBorins pushed a commit that referenced this pull request Nov 29, 2018
PR-URL: #23650
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@codebytere codebytere mentioned this pull request Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. doc Issues and PRs related to the documentations. meta Issues and PRs related to the general management of the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants