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

Add anonynous function tip #23

Open
Kikobeats opened this issue Aug 31, 2016 · 2 comments
Open

Add anonynous function tip #23

Kikobeats opened this issue Aug 31, 2016 · 2 comments
Labels

Comments

@Kikobeats
Copy link
Owner

Kikobeats commented Aug 31, 2016

reasons to name functions:

  • Stack Trace
  • Dereferencing
  • Code Reuse

Source: http://elijahmanor.com/talks/js-smells/#/11/3
Related: https://www.youtube.com/watch?v=_0W_822Dijg

@Kikobeats
Copy link
Owner Author

From https://hackernoon.com/how-to-make-the-fastest-promise-library-f632fd69f3cb

Avoid creating unnecessary variables, functions and instances
Following this principle helps avoiding unnecessary memory allocations. For example

function sum(array) {
  return array.reduce(function iterator(result, num) {
    return result + num;
  });
}


sum([1, 2, 3]); // 6

When sum is called, the iterator function is always created. It is one of unnecessary memory allocations. The code is rewritten to follow next example.

function iterator(result, num) {
  return result + num;
}

function sum(array) {
  return array.reduce(iterator);
}

sum([1, 2, 3]); // 6

The code avoids making unnecessary functions.

@mk-pmb
Copy link
Contributor

mk-pmb commented Mar 14, 2020

Another good reason to name a function, or to save some intermediate result in a named variable, is that it gives a free opportunity to describe your intent, if you're somewhat good with descriptive naming. Very useful for future maintenance programmers, probably your future self.

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

No branches or pull requests

2 participants