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

unnecessary uses of self #26

Closed
pixelzoom opened this issue Feb 10, 2020 · 6 comments
Closed

unnecessary uses of self #26

pixelzoom opened this issue Feb 10, 2020 · 6 comments

Comments

@pixelzoom
Copy link
Contributor

Related to #2 (code review):

  • If references are needed to the enclosing object, such as for a closure, self should be defined, but it should only be used in closures. The self variable should not be defined unless it is needed in a closure. Example: ...

There are 10 definitions of self, and they all appear to be unnecessary.

Example in GraphAccordionBox.js:

      Property.multilink( [ model.numVisibleMassesProperty, this.expandedProperty ], function( numMasses, isExpanded ) {
        graphContainer.children = normalModeGraphs.slice( 0, numMasses );
        graphContainer.children.forEach( graph => graph.update() );
        self.layout();
      } );

The use of self can generally be avoided by using arrow function notation (a JavaScript feature added in ES6). Arrow function do not create their own context, and this therefore refers to the enclosing context. In general, unless you need a context inside of your function, avoid function(... ) {...} and use (...) => {...}.

Using an arrow function, the above example would be:

      Property.multilink( [ model.numVisibleMassesProperty, this.expandedProperty ], 
        ( numMasses, isExpanded ) => {
          graphContainer.children = normalModeGraphs.slice( 0, numMasses );
          graphContainer.children.forEach( graph => graph.update() );
          this.layout();
        } );

Recommended to replace function with arrow functions throughout the code, and eliminate self.

@Hyodar
Copy link
Contributor

Hyodar commented Feb 11, 2020

No problem removing all uses and declarations of self by replacing function with arrow functions.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Feb 11, 2020

@Hyodar These changes are failing lint.

% cd normal-modes
% grunt lint
Running "lint" task

/Users/cmalley/PhET/GitHub/normal-modes/js/common/view/SpringNode.js
  62:26  error  Trailing spaces not allowed  no-trailing-spaces
  66:53  error  Trailing spaces not allowed  no-trailing-spaces

/Users/cmalley/PhET/GitHub/normal-modes/js/one-dimension/view/MassNode1D.js
  68:31  error  Unexpected parentheses around single function argument  arrow-parens
  89:51  error  Unexpected parentheses around single function argument  arrow-parens

/Users/cmalley/PhET/GitHub/normal-modes/js/one-dimension/view/NormalModeSpectrumAccordionBox.js
  218:46  error  Unexpected parentheses around single function argument  arrow-parens
  248:46  error  Unexpected parentheses around single function argument  arrow-parens

/Users/cmalley/PhET/GitHub/normal-modes/js/one-dimension/view/OneDimensionScreenView.js
  94:47  error  Unexpected parentheses around single function argument  arrow-parens

/Users/cmalley/PhET/GitHub/normal-modes/js/two-dimensions/view/AmplitudeSelectorRectangle.js
  77:33  error  Unexpected parentheses around single function argument  arrow-parens
  96:42  error  Unexpected parentheses around single function argument  arrow-parens

/Users/cmalley/PhET/GitHub/normal-modes/js/two-dimensions/view/MassNode2D.js
  73:31  error  Unexpected parentheses around single function argument  arrow-parens
  89:51  error  Unexpected parentheses around single function argument  arrow-parens

/Users/cmalley/PhET/GitHub/normal-modes/js/two-dimensions/view/TwoDimensionsScreenView.js
  78:49  error  Unexpected parentheses around single function argument  arrow-parens
  79:35  error  Unexpected parentheses around single function argument  arrow-parens
  87:49  error  Unexpected parentheses around single function argument  arrow-parens
  88:35  error  Unexpected parentheses around single function argument  arrow-parens

✖ 15 problems (15 errors, 0 warnings)
  15 errors and 0 warnings potentially fixable with the `--fix` option.
Fatal error: 15 Lint Errors

@pixelzoom
Copy link
Contributor Author

There are 2 types of errors above. They are both standard ESlint errors, described here:

no-trailing-spaces: https://eslint.org/docs/rules/no-trailing-spaces
arrow-parens: https://eslint.org/docs/rules/arrow-parens

@Hyodar
Copy link
Contributor

Hyodar commented Feb 11, 2020

Sorry, sorry! I've already fixed the #48 assertion issue, but I'm running into a bit of problems on my machine's grunt. I'll try to fix them as fast as I can.

Hyodar added a commit that referenced this issue Feb 11, 2020
@Hyodar
Copy link
Contributor

Hyodar commented Feb 11, 2020

Well, after some struggling with grunt, finally got to run it successfully here. I've actually reinstalled my OS recently, so I had to install it back. WebStorm reformat really wasn't getting those mistakes, it's something to keep an eye out for. Really sorry for the trouble.

@pixelzoom
Copy link
Contributor Author

No problem! Everything is working great now, so I'll go ahead and close this.

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

No branches or pull requests

2 participants