-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Question about code style: arrow function V.S. callback function #14496
Comments
@XadillaX - thanks. So your proposal is to select between the two based on the number of lines of code they encompass? The bearing the arrow functions have on their enclosure w.r.t receiver, arguments etc. and the necessity for using those bindings in the code should be the deciding factor in choosing one over the other right? |
On a second thought, if we are migrating from anonymous inliners to arrow functions, I guess there will not be any such thing as |
You don't explain why you think that. I can guess (and probably guess right) but it's better if you state it explicitly. |
In my opinion, in semantics arrow function stands for a inline lambda expression that input some arguments and do some simple logic to output a value. eg. A [ '', null, 1, undefined, -1 ].filter(v => !!v);
[ 1.1, 2.2, -1.2, -1.3 ].map(v => {
if(v > 0) return parseInt(v);
else return v;
}); And if we need a function to stand for a callback or a listener function, I think we should use an anonymous or named function. In that function we will do lots of logic whether it's synchronous or asynchronous and does not necessarily return data like a lambda expression. eg. fs.readFile(filename, function(err, data) {
// do something
// eg.
resp.send(data);
}); The most important point for me is 'in semantics'. I think in semantics arrow function stands for a lambda expression that input some arguments and do some simple logic to output a value. |
I saw some code where in my opinion ought to be anonymous function that written with arrow function. e.g. req.once('response', (res) => {
// ...
}); So I want to try to discuss about it and see if we can do any standardizing with them. |
IMHO I consider arrow function is something more like a inline closure or a scope, but not a function. So in semantics it's not suitable to use as a callback function or a listener. Sorry for my pool English that may not convey my opinion exactly. |
var ev = require('events').EventEmitter
function foo() {
this.buf = new Buffer(1024)
this.buf.fill('g')
}
foo.prototype.handle = function(e) {
// e.on('never', () => {this})
e.on('never', function() {this})
}
var e = new ev()
while (true) {
new foo().handle(e)
gc()
} This code shows the difference in terms of memory rentention - if you swap the normal listener for the arrow function, you see the memory shoots up - as the object foo() is held up with the arrow, while the listener only retains its closure context. While both leak memory in this scenario, the arrow causes more damage. So I guess the usage should be based on whether the continuation function needs the receiver obejct in the lexical scope or not - which may not be the case in most of the existing tests. So a best practice would be: Does this sound good? |
@gireeshpunathil Have you posted an issue on the V8 issue tracker about that? I'm not sure there should be such a difference... |
@gireeshpunathil Well the thing is, even though a human being can reason and say |
And I'd like to point out the fact that no one would write |
I don't agree that they should only be used for lambda like expressions. I use them pretty heavily for the non-binding of |
The point is - should we use it for non-binding of That is: convenient V.S. semantics. |
Looking back, I guesss my point was not clearly articulated. Let me try once again:
So in short as to when to use arrow functions, here is my opinion: |
But only because its body refers to I propose to close the issue because there is no consensus and no technical reason one is better than the other. |
I don't think @gireeshpunathil's point was what OP referred to. I believe what @XadillaX was arguing for is semantic correctness of arrow functions' original intent: to be lambda functions in a strictly mathematical sense, rather than just syntactic sugar for I don't necessarily agree with this. The introduction of |
I'll close this out since there is clearly no consensus. |
I want to discuss the code style of arrow function in Node.js source code.
Maybe most of you think that
=>
can replace all of anonymous callback functions. So Node.js source code includes lots of arrow functions. e. g.etc.
But IMHO, I think arrow function should only use in some situation that the function is just used as a inline lambda expression even though they have a very convenient feature - value of
this
. That means arrow function should not be misused. And anonymous function should used as callback (mainly asynchronous) or listener functions.So that's my opinion:
I think we can discuss with this and if my opinion is acceptable, I can start working with modifying the style.
The text was updated successfully, but these errors were encountered: