http,https: add default argument for Agent.prototype.getName - #41906
Conversation
|
Review requested:
|
| Agent.prototype.getName = function getName(options = {}) { | ||
| let name = options.host || 'localhost'; |
There was a problem hiding this comment.
I'd prefer if instead of creating a throw away object, we used optional chaining and set/keep the default value to undefined.
| Agent.prototype.getName = function getName(options = {}) { | |
| let name = options.host || 'localhost'; | |
| Agent.prototype.getName = function getName(options = undefined) { | |
| let name = options?.host || 'localhost'; |
There was a problem hiding this comment.
The option has many attributes, such as port,localAddress, family and so on. It will be more simplify to create a default object than make every attributes with ?.
There was a problem hiding this comment.
It will be more simplify to create a default object than make every attributes with
?.
Depends what you mean by simplify, it's a smaller change in term of source code changes, but it's not simpler for end users. Using a throw away objects also has side effects which could be surprising for them:
// In userland:
Object.prototype.host = 'something unrelated';
// In core:
function getName(options = {}) { return options.host || 'localhost'; }
console.log(getName()); // 'something unrelated'// In userland:
Object.prototype.host = 'something unrelated';
// In core:
function getName(options = undefined) { return options?.host || 'localhost'; }
console.log(getName()); // 'localhost'My opinion is that Node.js core modules should use optional chaining over creating a throw away objects, it's not a blocking concern by any mean though. Feel free to disagree, the PR is fine as is.
There was a problem hiding this comment.
Thanks for your detailed explanation. But I have seen many default arguments in nodejs is using empty object, I think we can draft another PR to fix it, if it is necessary. :)
|
No strong opinion but the |
|
@lpinca Thanks for your remind. I have changed the |
commented
Feb 11, 2022
add change log for agent.getName Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Co-authored-by: Mestery <mestery@protonmail.com>
commented
Feb 11, 2022
commented
Feb 12, 2022
commented
Feb 13, 2022
commented
Feb 27, 2022
Commit Queue failed- Loading data for nodejs/node/pull/41906 ✔ Done loading data for nodejs/node/pull/41906 ----------------------------------- PR info ------------------------------------ Title http,https: add default argument for Agent.prototype.getName (#41906) Author 小菜 (@xtx1130, first-time contributor) Branch xtx1130:fix-_http_agent-getName -> nodejs:master Labels http, https, author ready, commit-queue-squash Commits 4 - http: add default argument for Agent.prototype.getName - doc: change `agent.getName`'s argumnet to be optional - doc: Update doc/api/http.md - doc: Update doc/api/http.md Committers 2 - SindreXie - GitHub PR-URL: https://github.com/nodejs/node/pull/41906 Reviewed-By: Matteo Collina Reviewed-By: Ricky Zhou <0x19951125@gmail.com> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/41906 Reviewed-By: Matteo Collina Reviewed-By: Ricky Zhou <0x19951125@gmail.com> -------------------------------------------------------------------------------- ⚠ Commits were pushed since the last review: ⚠ - doc: Update doc/api/http.md ℹ This PR was created on Wed, 09 Feb 2022 08:49:26 GMT ✔ Approvals: 2 ✔ - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/41906#pullrequestreview-877174171 ✔ - Ricky Zhou (@rickyes): https://github.com/nodejs/node/pull/41906#pullrequestreview-879743293 ✔ Last GitHub CI successful ℹ Last Full PR CI on 2022-02-13T08:29:46Z: https://ci.nodejs.org/job/node-test-pull-request/42516/ - Querying data for job/node-test-pull-request/42516/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/1906354504 |
commented
Feb 27, 2022
|
Landed in 04c68ba |
http,https: add default argument for Agent.prototype.getName
Before:
after:
localhost::::::::::::::::::::::