Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upenforce function statements #381
Comments
This comment has been minimized.
This comment has been minimized.
|
Could you post some example code? Probably easier to self-parse and understand what you're referring to? |
dcousens
added
the
question
label
Jan 12, 2016
This comment has been minimized.
This comment has been minimized.
|
Hi Daniel, Thanks for your reply. I'm generally referring to 'private' helper type functions that don't have value outside of their enclosing function. Take the Here a function expression or declaration would suffice. For consistency, which one should I opt for? /*
* Example adding a comment with an optional replyToId
*/
function addComment (body, inReplyToId, callback) {
// Define the save function with a function declaration
function save (comment) {
comment.save(function (err) {
if (err) {
callback(err)
}
callback(null, comment)
})
}
// The above function could have been a function expression...
// var save = function (comment) {
// comment.save(function (err) {
// if (err) {
// callback(err)
// }
// callback(null, comment)
// })
// }
if (inReplyToId) {
Comment.findById(inReplyTo, function (err, doc) {
if (err) {
callback(err)
}
save(new Comment({ body: body, inReplyTo: doc.id }))
})
} else {
save(new Comment({ body: body }))
}
}Hope this helps Dave |
This comment has been minimized.
This comment has been minimized.
|
I personally opt for Maybe there should be. @feross ? |
This comment has been minimized.
This comment has been minimized.
|
xo has one. I don't mind it. |
This comment has been minimized.
This comment has been minimized.
|
Likewise I have opted for More recently though I have noticed people using While I don't have a strong preference, I would like to see some consistency within code base/across teams. |
This comment has been minimized.
This comment has been minimized.
Probably because you have to use that syntax if you are using context capturing lambdas: var save = () => {} |
This comment has been minimized.
This comment has been minimized.
|
I use arrow functions with callbacks or when I need to preserve context. I'm not sure this holds true anymore, since it seems most engines have improved this by now... But it used to be that the following would give you worse stack traces: // Anonymous function
const foo = function () {}
// Could be fixed by giving it a name
const foo = function foo () {}With babel the default behavior seems to be to name the function: // Before
const foo = () => {}
// After
const foo = function foo () {}I'm a fan of function declarations since it tends to encourages writing pure functions. |
This comment has been minimized.
This comment has been minimized.
|
Function declarations are preferred. Function declarations have more intuitive hoisting behavior, i.e. you can use the function anywhere that's in-scope. With function expressions, only the variable is hoisted, so you can only use the function after the line where the assignment occurs. |
This comment has been minimized.
This comment has been minimized.
|
Just tried enabling
Full output: https://gist.github.com/feross/0d57fdddde82f55d618a It's a high number, so leaning toward not enabling this. Much as I like it. Maybe if we do |
feross
added
feature request
and removed
question
labels
Feb 4, 2016
feross
changed the title
function expression vs statement
enforce function statements
Feb 4, 2016
This comment has been minimized.
This comment has been minimized.
|
For documentation, here's the rule I tested with: "func-style": [2, "declaration", { "allowArrowFunctions": true }] |
feross
added
enhancement
and removed
feature request
labels
Feb 4, 2016
This comment has been minimized.
This comment has been minimized.
|
@feross how about V6? I think it is a reasonable enough change and absolutely in the spirit of our existing rules. |
This comment has been minimized.
This comment has been minimized.
|
@dcousens Agreed, but it's quite a bit of breakage (22% of the ecosystem). |
This comment has been minimized.
This comment has been minimized.
|
@feross it is also a break that could probably be resolved with a regex. We could provide / run it for them even. |
This comment has been minimized.
This comment has been minimized.
|
For VIM users %sno/var \(\.\+\) = function \.\*(/function \1 (/gc |
This comment has been minimized.
This comment has been minimized.
|
closing - we can move discussion of potential "next gen" or big breaking rule to the standard-strict issue: #382 |
davidjamesstone commentedJan 11, 2016
I was wondering about folks preference in regards to expression vs statements.
Clearly it's important to understand the differences between the two in regards hoisting and also the use cases of each but there are often times where either could be used.
I can't find a recommendation - is there a guideline or for this in standardjs?
I see there's the "Enforce Function Style (func-style)" rule in ES Lint. I don't think I'm suggesting this be implemented but it would be nice to have an official line on the preferred approach.
Many thanks
Dave