Skip to content
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

Libby's Ruby module attempt #1

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

emhoracek
Copy link
Member

@emhoracek emhoracek commented Nov 5, 2018

I wanted to learn about modules, so I tried using a module. Not sure if it was the right way to go...

Would also appreciate any feedback about Rubyisms, code style, etc. Nitpicking welcome!

There aren't many tests mostly because I was using the Gold Master (provided by the exercise) as I refactored.

Description problem: http://iamnotmyself.com/2011/02/13/refactor-this-the-gilded-rose-kata/

Copy link

@toddmazierski toddmazierski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! One last little tip is to run the file through RuboCop, which even with the default rules may have a few good suggestions.

end

class ItemUpdater
include Updater

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I really dig this approach! My main point of feedback is although we're not technically using inheritance (ex. class ItemUpdater < Updater), including the same module in each of these classes is effectively the same thing. If this feels like a good design, I'd just commit to using actual inheritance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed that too! It really feels like this exercise "wants" classical inheritance.


def decrease_quality
if quality > 0
item.quality = item.quality - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit-pick, but this could be shortened to item.quality -= 1, and further shortened to:

item.quality -= 1 if quality > 0

end

module Updater
attr_accessor :item

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a public interface? If not, it could be moved to private.

include Updater

def update_sell_in
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit-pick, but you can further express the "no-op"-ness of this method by saying: def update_sell_in; end on one line. It's sort of a way of saying "this page intentionally left blank" .

end

class ItemUpdaterFactory
def self.create_updater_for(item)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit-pick: I think the verb "build" is more often used in the context of factories, instead of "create".

item.quality
end

def increase_quality

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Rubyism is adding a bang to methods that have side-effects like mutation. So, increase_quality! might be a better name.

A warning, though: Ruby devs go overboard with them, so use sparingly. 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The side-effect-iness of this solution in general feels kinda odd to me. I mostly kept the original code and functionality and just put it in different places, so I didn't try to make it non-side-effecting. As it is, I think just about every method needs a !!

include Updater

def update_quality
2.times { decrease_quality }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool. 👍

end

class ItemUpdaterFactory
def self.create_updater_for(item)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be a static method on Updater if you want to reduce the number of classes flying around.

"Backstage passes to a TAFKAL80ETC concert" =>
BackstagePassesItemUpdater.new(item),
"Conjured" => ConjuredItemUpdater.new(item)
}.fetch(lookup_name(item), ItemUpdater.new(item))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love a good map ^_^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making the Conjured thing a separate routine to try when the name match fails. It could be a big confusing to read when certain strings in the map are name matches and another is actually a match on a substring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhartunian I was definitely thinking of you as I mapped this map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants