-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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: moved lastCategory assignment inside the if condition for efficiency and logic #6391
Conversation
Hi @matys1! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Size changes📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
@sophiebits @lunaleaps @smikitky @rickhanlonii @mattcarrollcode @A7med3bdulBaset could someone please give a quick glance to this PR? I think this is a good and valid change. |
I'm not an author of the original docs (I'm just a translator), but since you mentioned me...
|
Thanks @smikitky. I tend to agree: both versions of the code will run with similar performance so it is a question of which one is clearer. I can see arguments either way but I don’t think it is decisive so I will leave it as is. |
I strongly disagree and you didn't provide a reason as to why you think the current version is clearer. The readability of code is the main reason why I submitted this PR. The current version is objectively less efficient than my proposal because you update lastCategory unconditionally and unnecessarily on each iteration. But let's put performance aside because you're right for this trivial example performance doesn't matter. What matters is readability, especially because the "Thinking in React" paper is read by beginners. I provided GPT-4 with both versions of the "Version 2 is more logical for a reader because it groups the action of appending a category row and updating the lastCategory state together. This proximity makes it clearer that these two actions are related, which is beneficial for beginners trying to understand the flow of the program. For beginners, the logical grouping and proximity of related code make a significant difference in understanding the code's intent. It teaches them good practices about updating related state as close to each other as possible." A the end its up to you guys but I just don't understand why would we reject a PR that makes code both more efficient and easier to read and understand, especially for beginners. |
I don't agree that it is definitely clearer. You could think of it as either In a., These are of course equivalent in this case but I think you could have either mental model. The current code does a., while yours does b. |
Sophiebits explained this better than I, but this is a matter of whether the intended meaning of |
My thought process was that So while I think I understand the point about the alternative semantic interpretation of the Apologies for tagging a bunch of potentially unrelated folks. I not sure who wrote the original example and how to officially request reviewers. |
I know, and I'm saying that's just one of the two equally valid thought processes. After all, as sophiebits said, "I can see arguments either way but I don’t think it is decisive". If you cannot imagine what the other argument would look like, here it is:
I don't want you to misunderstand this; I have no intention of deciding which thought process is superior. It is sufficient to show that this is a debatable issue with no definitive answer. |
An experienced developer might write this without any "imperative" stuff like const rows = products.flatMap((p, i) => [
p.category !== products[i - 1]?.category && (
<ProductCategoryRow category={p.category} key={p.category} />
),
<ProductRow product={p} key={p.name} />,
]); As you can see, there is no need to introduce a variable to remember the category of the last heading in the first place. The original code doesn't use |
Thanks for your explanation. I think I was reading the paper with the mindset or expectation that things will be closely coupled and if things were not closely coupled there would be a very good reason for it, like some other piece of code relies on the current state of However, with saying all that I understand your argument. Indeed unconditionally tracking the last category of each item in a loop is a more data-centric approach (as opposed to the UI-centric approach I suppose) and in some ways could also be seen as a "simple" solution to this problem. And indeed you can abstract the logic even more concisely (and perhaps more elegantly) using the logical While I still prefer my proposal over the original I've come to appreciate that there are devs out there that have very different perspectives and approaches to the same problem. I've spent way more time on this PR than I anticipated (with a lot stronger pushback that I anticipated :)) so I rest my case. |
In
thinking-in-react
movedlastCategory = product.category
inside theif (product.category !== lastCategory)
condition which is more efficient and logical. This ensures thatlastCategory
is only updated when a new category is encountered, which is precisely the condition under which you want to update it.