Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V8 recommend using the
MaybeLocal
returning version.(IMHO it's also cleaner then the out-param version)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the
MaybeLocal
version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So why not:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this way is easier to read and reason about (don't really have to think about
Maybe
s).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO having an uninitialized variable is code smell, and if there's a way to avoid it, why not.
Also
if (!ctx->sandbox()->GetPropertyNames(ctx->context()).ToLocal(&properties))
is a bit busy it does like 5 things...But I leave it to the author.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@refack Maybe a more objective advantage of the current variation here: It’s easier to handle errors from sequences of operations this way:
node/src/node.cc
Lines 1237 to 1241 in 8ce99fa
At first, I also was a fan of what you’re suggesting, but for cases like these it can become quite annoying…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's IMHO an extreme example of this pattern. What's wrong with:
The original code also has short-circuit logic, so it is tricky to reason about.
P.S. see the
// Exception pending
comment, can we tell what when wrong?We have actually have several guidelines that say to avoid this:
Google's:
Prefer using return values rather than output parameters. If output-only parameters are used they should appear after input parameters.
C++CG:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn’t compile …
MaybeLocal
s sadly don’t form a full monad; you can’t actually call->Get()
or->ToObject()
on one. The idea, as I understand V8, is to force API users to return to JS as soon as possible, and not attempt to do more operations.No, but ideally we don’t have to care about that anyway.
It’s also a bit odd to quote Google’s style guide here… I totally see what you mean and why this pattern can be counterintuitive, but after all, this pattern is of Google’s making and V8 uses it very extensively in its own code. 😄
I’d say that rule explicitly lists this pattern as an exception:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to read this to figure out if the values are dependant. I looked several times, but missed it. That's a red light for me.
Anyway like you say, we write code so it reads well, not writes easy. The following is linear and reads much better, with only one action per line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ThrowException()
bit is not quite howMaybeLocal
s work – an empty one typically means that there is already an exception pending – but that’s a fair point, yes.To be clear, I don’t personally like the repetition there, but I wouldn’t object to anybody using individual checks for each call.