Merged
Conversation
damassi
reviewed
Feb 3, 2021
| jest.mock("@artsy/stitch", () => ({ | ||
| stitch: jest.fn(), | ||
| })) | ||
| const stitch = require("@artsy/stitch").stitch as jest.Mock |
Merged
Member
|
Oh wow, I somehow missed this PR — but this always bugged me; very nice! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR alters force to redirect like so:
/log_in=>/login/sign_up=>/signupThis change is motivated by some SEO guidance but it's also been an opportunity to do some refactoring which is why this PR is 12 commits, haha.
Forgetting What We Already Knew
While reading through the auth code to make this change, I found a familiar anti-pattern that I refer to as "forgetting what we already knew". Check out this routing code:
This code starts by telling Express that we want to handle three paths and sends them each to a single handler. That handler then examines the path on the request to know which type we're working with. This is bananas! We already knew what type of auth we were dealing with when Express picked up the route and called the handler but then we forgot it by routing them all through a single callback!
The refactor here is to extract a handler for each path and DECLARE the values that are specific to that path and then call shared code. This allows us to remove conditionals that are specific to a particular path. Removing ifs and case statements is a great way to reduce the complexity of a piece of code.
Finding Seams When SRP is Violated
There's another, bigger problem with the code I found and it's a bit harder to see at first. When I started reading the code that handled the three requests I was overwhelmed. What was signal and what was noise? This function was violating the Single Responsibility Principle, that much was clear, but how could I improve it?
I turned to the tests hoping that they could help unravel what was going on. What I saw there were a bunch of tests asserting about the arguments being sent to a particular function call. We used jest to mock calls to stitch and then assert about the various intricacies of the object being sent to it.
Ah ha! The true responsibility of the handler function is to call
stitchand we're very concerned with the shape of the arguments. This allowed me to see that there was a missing seam - we could extract a function that computes the options being sent tostitchand then reduce the noise in our handlers by encapsulating all the complexity of request parsing, etc in this compute function.When to Stop Refactoring
I could have done so much more refactoring but I felt this was a good place to stop because it was a coherent piece of work. I wanted to make a change so I refactored to make that change easier and then I made that change. Beck would be so proud of me!
My next step will be to examine more closely how the meta part of this works because I need to set canonical tags differently. That will cause me to bump into a few things that I'm interested in improving so I can pick back up on this line of work then.
https://artsyproduct.atlassian.net/browse/GRO-18
/cc @artsy/grow-devs