-
Notifications
You must be signed in to change notification settings - Fork 8
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
Refactor part processing (remove PartProcessor) #31
Refactor part processing (remove PartProcessor) #31
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'm loving these small improvements! 🚀 I just left a small question on one part otherwise looks good.
def process_parts(parts : Array(String), method : String, payload : T) | ||
PartProcessor(T).new(self, parts: parts, method: method, payload: payload).run | ||
self | ||
leaf_fragment = parts.reduce(self) { |fragment, part| fragment.add_part(part) } |
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.
Nice!
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 really like this and I love the idea of wrapping the part
in a class. That should make it a lot easier to further clarify the code. Nice!
Just some small comments on an explaining variable and removing an early return
src/lucky_router/fragment.cr
Outdated
name: part[1...], | ||
fragment: Fragment(T).new | ||
) | ||
return dynamic_fragment.fragment |
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.
For some reason my brain has a really hard time following early returns. Could this be refactored to use conditionals and implicit returns instead?
src/lucky_router/fragment.cr
Outdated
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( | ||
name: part[1...], | ||
fragment: Fragment(T).new | ||
) |
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 took me awhile to figure out what what this was doing and why and then realized it was removing the first character which is the :
. Thoughts on adding this explaining variable to clarify what it is doing?
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( | |
name: part[1...], | |
fragment: Fragment(T).new | |
) | |
part_without_colon = part[1..] | |
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( | |
name: part_without_colon, | |
fragment: Fragment(T).new | |
) |
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 kind of thing is why I want to wrap the part strings in a part class.
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.
Yeah I was thinking that when I read your comment. That will be a really awesome refactor 👍 Nice idea
@paulcsmith I have implemented your changes |
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.
Lovely! Thanks for the awesome refactor (and speed boost)
Summary
This change removes the
PartProcessor
class completely and moves the logic into theFragment
class. I believe this brings clarity to the codebase as the logic using the processor required passing in the fragment and all of the passed in information anyways and it was calling methods on the fragment. Along with the move of logic, two changes were made:.reduce
call on the array of parts. Besides being easier to understand (I hope), it is responsible for the majority of the speed up below.add_part
method so that at the higher level ofprocess_parts
there is no difference.Benchmark (with
--release
)