-
Notifications
You must be signed in to change notification settings - Fork 4.8k
created function transform state with clones #5233
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
base: master
Are you sure you want to change the base?
Conversation
mateacademy-ai-mentor
left a comment
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.
Review Summary
Great job implementing the core logic for transforming the state! The function correctly handles the different action types and avoids mutating the original state. However, a few changes are needed to align with the project's coding standards before this can be approved.
Here are the key areas to address:
- Error Handling: The
defaultcase in yourswitchstatement should be more robust. Instead of resulting in anundefinedstate for unknown actions, it should throw an error to prevent potential bugs down the line [CHECKLIST ITEM #3]. - Redundant Code: To follow the DRY principle, please create a copy of the state before the
switchstatement, rather than within eachcase. This avoids repeating the cloning logic [CHECKLIST ITEM #4]. - Naming: The parameter name
cloneisn't very descriptive. A name that better reflects its purpose, such assourceObjectorpreviousState, would improve readability [CHECKLIST ITEM #5].
Once these points are addressed, your solution will be in great shape!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
src/transformStateWithClones.js
Outdated
| default: | ||
| resultObject = undefined; | ||
| break; |
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.
This violates checklist item #3: 'switch/case should always have default case for error handling'. While a default case is present, assigning undefined to resultObject for an unknown action type can lead to subtle bugs. A more robust way to handle this error would be to throw an exception, for example: throw new Error(Unknown action type: ${type});.
src/transformStateWithClones.js
Outdated
|
|
||
| module.exports = transformStateWithClones; | ||
|
|
||
| function removeProperties(action, clone, resultObject) { |
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.
This violates checklist item #5: '[NAMING] - use proper names for object copy'. The parameter name clone is not very descriptive. A name that better reflects its role, such as sourceObject or previousState, would improve code readability.
mateacademy-ai-mentor
left a comment
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.
Review Summary
Excellent work on this task! Your function correctly handles all action types, maintains immutability, and includes robust error handling as required. I am approving your solution because it meets all the core functional requirements.
For future improvement, consider how to avoid repeating code. In your switch statement, both the 'addProperties' and 'removeProperties' cases make a copy of the state. A more streamlined approach would be to make each case responsible for creating the entire new state object. This would make the logic for each action clearer and adhere more closely to the DRY (Don't Repeat Yourself) principle mentioned in [CHECKLIST ITEM #4].
Overall, this is a solid implementation. Well done!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
src/transformStateWithClones.js
Outdated
| for (const action of actions) { | ||
| const type = action.type; | ||
| const lastChangedObject = resultedArray.at(-1) || state; | ||
| let resultObject = {}; |
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.
This approach of initializing an empty object and populating it in the switch leads to repeated logic, which violates checklist item #4 ('[DONT REPEAT YOURSELF]'). Both the 'addProperties' case and the 'removeProperties' case need to make a copy of lastChangedObject.
Consider making each case responsible for creating the entire new state object and assigning it to a variable. This makes the logic for each action clearer and avoids duplicating the copying step.
No description provided.