-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix duplication of available bin commands (#1991) #2019
Fix duplication of available bin commands (#1991) #2019
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd rather de-duplicate the bin folder paths of the available registries beforehand. This keeps the concerns better separated than doing this inside a for-loop. You could even move this into a small, well-named function then :)
I'd love to - however, there's no duplication of the paths inside each registry, so this check has to be done in a for-loop anyway. Or maybe I misunderstand the suggestion? |
Let me phrase it differently: I'd do a small loop to construct an array of paths which could be de-duplicated with something like uniq before running the actual loop. This would be one way to solve it in a slightly more functional programming approach (which I think |
Like this? a968fd0 |
Yeah, I think this is better. You have to be careful though: I think moving the for (const registry of Object.keys(registries)) {
...
} loop into a well-named function which takes |
@hpurmann I think previous approach was simple & clean. |
Sure, I'm fine with that too. I just think that the overall codebase could profit from a more functional style. But yeah, consistency-wise, the first solution fits perfectly ;) |
@hpurmann How using two loops instead of one is more functional? Or, I'm missing something here? |
I think here instead of |
This reverts commit a968fd0.
So let's keep it simple :) I've reverted the code back to original version. Could you please accept the PR? |
@@ -31,13 +31,17 @@ export async function run( | |||
const scripts = map(); | |||
const binCommands = []; | |||
let pkgCommands = []; | |||
let visitedBinFolders = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use const
instead of let
.
And, please run yarn lint
before submitting a PR 😄
@DenisGorbachev Sorry to bother you for one last time. Can you please just use |
@torifat sure, changed to |
Thanks for your contribution 😄 |
@torifat Even though this is merged already and discussions about programming styles might lead nowhere, I still want to explain my comment and answer yours.
Ok, functional programming might be a bit far-fetched. I want to keep concerns separated. I want to call well-named pure functions instead of mutating and accessing the same data structure on different lines. To give an example: let visited = []
for (const registry of registries) {
let path = getPath(registry)
// Do stuff here
if (!visited.contains(path)) {
// Do stuff here
visited.push(entry)
}
} There are three lines where we access In constrast, the example below uses a function to first collect and return the registry entries (which would probably be using a loop internally). Afterwards, we remove the duplicates. const availablePaths = getPathsfromRegistries(registries)
const uniquePaths = uniq(availablePaths)
uniquePaths.map((path) => {
// Do stuff here
}) or even easier, by using const uniquePaths = flow(
getPaths,
uniq
)(registries) When looking at the actual example, there is no need to iterate over |
@hpurmann I'm an advocate of this pattern. And, I use things like
The example you have given in your last comment and the change (a968fd0) you said better are not same. I also prefer using lots of small functions instead of nested loops and if/else. Check this: https://github.com/yarnpkg/yarn/blob/master/src/cli/commands/upgrade-interactive.js#L56-L116 I hope you get my point. |
Summary
This PR solves #1991.
Test plan
I've modified
__tests__/commands/run.js
to reflect the fixed behavior.Also, I ran all test suites: