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

declare functions #34

Open
thepian opened this issue Oct 30, 2014 · 2 comments
Open

declare functions #34

thepian opened this issue Oct 30, 2014 · 2 comments

Comments

@thepian
Copy link

thepian commented Oct 30, 2014

How about function declarations? It drives me nuts when I see

var packAssets;

packAssets = function(...) {
return packed;
}

I don't understand why people do this. If you call it before the declaration packAssets will be undefined.

@kentcdodds
Copy link
Contributor

I almost always prefer function declarations over function expressions (see this). So...

function packAssets(...) {
  return packed;
} // also, notice no semicolon, that's because it's incorrect to have one here

And I generally will put all my function declarations at the bottom of the closure to make my code more readable:

module.exports = {
  packAssets: packAssets
};

function packAssets(...) {
  return packed;
}

Doesn't really buy you much when you only have one function, but after about 3 or 4 it makes it a lot easier to know what the module's api is without having to scroll at all.

Function declarations also improve your stack trace because the function has a name whereas a function expression is nameless unless you explicitly give it one, which people rarely do.

These are my opinions though, curious to hear @felixge weigh in.

@felixge
Copy link
Owner

felixge commented Oct 31, 2014

The guide already has a bunch of examples on how functions should be declared, but they are given in the context of other rules. I wouldn't mind an explicit section for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants