Skip to content

Conversation

@kytrinyx
Copy link
Contributor

@kytrinyx kytrinyx commented Dec 9, 2012

Hash.from_xml was very complicated, and so we decided to take a stab at
refactoring it. This is a work in progress that we want to get feedback on.

Basically, instead of defining a huge method on Hash, we made a utility
object for actually doing the conversion. This let us break up the big
method into a bunch of smaller ones, so that the process overall is
easier to understand.

However, some of the names kinda suck. Still working on that. But we
wanted to get some feedback on if this kind of change is generally
considered to be better or not before putting more effort into
breaking it up. We think that a bunch of small methods are easier to understand
than one massive one.

Things to work on:

  • Names. {process,convert} is awkward. And there's also typecast/to.
  • Might be another utility object as well, as half of the class is for refining
    a Hash that's getting converted.
  • We may have uncovered either missing test cases or extraneous conditions
    in a few of the if statements. We've left some comments. Tests pass if
    you remove them, but we didn't want to before confirming that they
    aren't actually needed.
  • Changes to documentation, including # :nodoc: where appropriate

❤️

Copy link
Member

Choose a reason for hiding this comment

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

I think that is better to use a case statement here instead of call send based in the class name.

Copy link
Member

Choose a reason for hiding this comment

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

Really? Hm. I guess it's not the worst, and there's only 4 of them...

Copy link
Member

Choose a reason for hiding this comment

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

I don't know, seems weird to me.

case value when Array convert_array(value) when Hash convert_hash(value) else raise end

This reads better to me. It is cleaner. It make clear what are the supported types of value without need to read the whole class.

@rafaelfranca
Copy link
Member

I like the idea to have a specialized object to do the conversion. 👍 from my side

Choose a reason for hiding this comment

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

This comment could 🔥

Copy link
Member

Choose a reason for hiding this comment

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

yeah it was originally there. I think that maybe the code was changed and not the comment.

@carlosantoniodasilva
Copy link
Member

Nice stuff @kytrinyx @steveklabnik 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

# :nodoc:

Copy link
Member

Choose a reason for hiding this comment

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

Totes.

Copy link
Member

Choose a reason for hiding this comment

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

Changing this is the line that broke the test. ha!

@steveklabnik
Copy link
Member

We've updated this pull request to handle all of the feedback.

We re-checked that we didn't break anything when making these changes, and rolled back a few of the more aggressive changes we made previously.

We're thinking that, since most people don't actually use this code, in the future, it might be a good candidate to extract to a plugin.

@rafaelfranca
Copy link
Member

:shipit:

@kytrinyx
Copy link
Contributor Author

Should we put the XMLConverter class in its own file?

@steveklabnik
Copy link
Member

Since it's probably not going to be used on its own, this is fine. Having it in its own file would be nice if it needed to be autoloaded, but it won't.

I'm going to squash and then :shipit:.

Three basic refactors in this PR: * We extracted the logic into a method object. We now don't define a tone of extraneous methods on Hash, even if they were private. * Extracted blocks of the case statement into methods that do the work. This makes the logic more clear. * Extracted complicated if clauses into their own query methods. They often have two or three terms, this makes it much easier to see what they _do_. We took care not to refactor too much as to not break anything, and put comments where we suspect tests are missing. We think ActiveSupport::XMLMini might be a good candidate to move to a plugin in the future.
steveklabnik added a commit that referenced this pull request Dec 21, 2012
WIP Refactor xml conversion to hash
@steveklabnik steveklabnik merged commit 10c0a3b into rails:master Dec 21, 2012
@kytrinyx kytrinyx deleted the refactor-xml-to-hash branch December 21, 2012 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

6 participants