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

Improve coding style (general) #786

Closed
brodycj opened this issue Jul 17, 2019 · 10 comments
Closed

Improve coding style (general) #786

brodycj opened this issue Jul 17, 2019 · 10 comments

Comments

@brodycj
Copy link
Contributor

brodycj commented Jul 17, 2019

Some ideas that I raised in review of PR #764, some of which may apply to other Cordova packages:

  • use JavaScript array methods such as forEach, map, and reduce to get rid of for loops
  • in case of JavaScript objects, use keys method to get the array of keys then use array methods to get rid of for loops
  • use const (first choice) or let (second choice) instead of var
  • use ternaries to get rid of if blocks
  • use nested ternaries to get rid of multiple if blocks (possibly controversial)
  • use utility packages such as recursive-readdir-sync whenever possible instead of shelljs and to reduce the amount of “fs*” code we have to maintain

I am raising this issue before it gets forgotten. Maybe we should transfer or write this issue in https://github.com/apache/cordova/issues for larger discussion and consideration.

@janpio
Copy link
Member

janpio commented Jul 17, 2019

Good idea to start a discussion on this, but I agree this can probably be a Cordova level discussion.

Can you please annotate the individual suggestion with the connected Node/ES requirements so the impact of these changes become clearer?

Other discussions about what you suggest can happen when the issue is in the right place and properly framed. Thanks.

@dpogue
Copy link
Member

dpogue commented Jul 17, 2019

  • use ternaries to get rid of if blocks
  • use nested ternaries to get rid of multiple if blocks (possibly controversial)

I pretty strongly disagree with these suggestions, especially nested ternaries. Cordova code is already often hard to follow, and ternaries make it even harder to read and understand.

@breautek
Copy link
Contributor

breautek commented Aug 3, 2019

I noticed that the cordova projects use eslint but doesn't take advantage of shareable configs.. At my company we have several repositories where we want to use a consistent eslint configuration. To ensure this, we actually created an eslint config module.

Basically you name the module, eslint-config-<yourconfig>, so in cordova's case... it would be eslint-config-cordova I guess, and you can define all your rules inside this module, which all cordova projects & plugins would include as a dev dependency.

"eslint-config-semistandard": "^13.0.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",

These eslint plugins and configs if they apply across all cordova packages can also be dependencies of our custom config module therefore every cordova package doesn't need to install each package manually either.

@janpio
Copy link
Member

janpio commented Aug 4, 2019

(Another thing for the more generalized version of this issue that probably didn't get created yet)

@breautek
Copy link
Contributor

breautek commented Sep 9, 2019

@janpio I recently discovered that https://github.com/apache/cordova-discuss is a place to suggest proposals? Would this make sense to do for the eslint config I idea I mentioned above?

@janpio
Copy link
Member

janpio commented Sep 9, 2019

IMHO this is not really the case, but some people seem to use it that way, so why not. (The official location would be the dev mailing list with a link to a thought out description either in mail or via link)

@breautek
Copy link
Contributor

I would also strive to add "use strict;" in the javascript sources, and fix any code that would be preventing us from using strict mode.

Strict mode offers 2 main benefits:

  1. Enables optimizations in the JS engines to make JS perform faster whereas without strict mode, the JS engines cannot possibly make such assumptions during its interpretations.
  2. Helps prevent some JS bad practices being added in the future as most of them will lead to actual errors.

@brodycj
Copy link
Contributor Author

brodycj commented Oct 15, 2019

Any objections if we transfer this to https://github.com/apache/cordova/issues?

I think some of these ideas are covered by apache/cordova#142.

@brodycj
Copy link
Contributor Author

brodycj commented Oct 15, 2019

[...] add "use strict;" [...]

I think we should do this, since we are still not using ES6 modules.

Node.js is in a strange situation where it does support ES6 but not ES6 modules unless we start using an experimental flag along with some other changes as documented in https://nodejs.org/api/esm.html. (This may change in Node.js 12 LTS according to https://2ality.com/2019/04/nodejs-esm-impl.html#using-es-modules-on-node.js.)

Maybe this should be a separate discussion in https://github.com/apache/cordova/issues?

Any objections if we transfer this to https://github.com/apache/cordova/issues?

I tried to do this, GUI does not seem to let me select apache/cordova.

@jcesarmobile
Copy link
Member

closing in favor of the linked issue in cordova repository

@jcesarmobile jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2023
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

5 participants