-
-
Notifications
You must be signed in to change notification settings - Fork 240
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-implement-and-rewrite #713
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: main
Are you sure you want to change the base?
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-implement-and-rewrite #713
Conversation
…jest/2-is-proper-fraction.test.js modified: Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js modified: Sprint-3/2-practice-tdd/count.test.js
illicitonion
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.
This is generally looking good, but I left a few things to look at
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Outdated
Show resolved
Hide resolved
| } else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) { | ||
| return Number(rank); | ||
| } else { | ||
| throw new Error("Invalid card rank"); |
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.
It can be useful to include what the problematic data was in an error message, so that someone knows what they should be debugging - can you give that a go?
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.
Yes you are correct it would be helpful/useful to include the problematic data in an error message and we can achieve this by doing.
throw new Error('Invalid card rank: "$(rank)"');
| } else if (["K", "Q", "J", "10"].includes(rank)) { | ||
| return 10; | ||
| } else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) { |
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.
You could have handled "10" in either the K/Q/J case, or the 2-9 case - which do you prefer? What advantages/disadvantages do you think each has?
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.
hello,
Thank you for the feedback, I prefer to handle the "10" in the K/Q/J case. for clarity and explicitness, and to maintain semantical grouping since "10" has the same card value as the face cards. a disadvantage could be mixing string handling logic "K/Q/J" with numeric checking logic "10".
We can also handle "10" in the 2-9 case and the pros for this would be logical consistency the numeric branch handles anything numeric between 2-10. and its easier to extend if we ever want to adjust the ranges to include 1 or 11 or validate different ranges we just change the numeric branch. Disadvantages risk of mis-parsing since !isNaN(rank) is loose we might accidently accept strings like "10abc". Edge-case handling we need to make sure to handle trimming, ect. in case someone passes weird strings like "010" and " 10 ".
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.
Sounds good, thanks for sharing your thoughts!
Personally I prefer 10 to be included in the number case, because it leads to simpler code. In the numeric case, we already need an upper bound, so the difference between Number(rank) <= 9 and Number(rank) <= 10 is really small, whereas adding a whole extra element to the array we're checking is adding an extra piece: ["K", "Q", "J", "10"] vs ["K", "Q", "J"].
But you're right that there's not one obviously better answer - there are advantages and disadvantages to each :)
| expect(getCardValue("A♥")).toEqual(11); | ||
| }); | ||
| // Case 5: Handle Invalid Cards: | ||
| test("should return null for invalid cards", () => { |
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 test looks like it's expecting something different from your previous test - before it was checking it throws, here it's checking for null. Does the same code pass both tests? Should the tests look more similar?
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 have modified the case 5: test to check it throws error rather than checking for null. I have also run a test with pass.
illicitonion
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.
Looks good! Just one thing to fix up and this is done!
| console.assert(false, "Expected an error to be thrown for invalid card rank"); | ||
| } catch (e) { | ||
| console.assert( | ||
| e.message === 'Invalid card rank: "${rank}"', |
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.
There's a tension in testing between calculating expected values in tests (to avoid duplication), or hard-coding expected values (to make it really obvious that the test is correct).
Can you change this test so that it actually includes "1" in the expected error message:
| e.message === 'Invalid card rank: "${rank}"', | |
| e.message === 'Invalid card rank: "1"', |
I think you'll find that the test is actually failing, so we should fix the code :)
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.
Thank for the feedback and I have now included "1" in the expected error message. I have run the test with a pass.
thank you for the suggested change.
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 just ran the test and it failed...
% node Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
Assertion failed: Expected error message to be "Invalid card rank" but got "Invalid card rank: "${rank}""Can you check again?
(Also, your Jest tests wouldn't catch this same bug, can you fix them up so they do as well?)
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.
Thank you for picking on that and being patient with me. test was not failing on my side because Jest’s .toThrow("Invalid card rank") only checks that the error message contains that substring, not that it matches exactly. now I have updated the code and it passes as it should.
| } else if (["K", "Q", "J", "10"].includes(rank)) { | ||
| return 10; | ||
| } else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) { |
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.
Sounds good, thanks for sharing your thoughts!
Personally I prefer 10 to be included in the number case, because it leads to simpler code. In the numeric case, we already need an upper bound, so the difference between Number(rank) <= 9 and Number(rank) <= 10 is really small, whereas adding a whole extra element to the array we're checking is adding an extra piece: ["K", "Q", "J", "10"] vs ["K", "Q", "J"].
But you're right that there's not one obviously better answer - there are advantages and disadvantages to each :)
illicitonion
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.
Looks great, thank you!
Self checklist
completed the coursework/sprint-3 implement-and-rewrite by answering the questions in the tasks and implementing test-cases and making sure all test-cases have passed.