Yesterday I was looking at some Ruby code that had an initialize
method like
this:
|
This code has hidden intent that could be easier to read. By assigning
@data
possibly twice it forces the reader to keep extra context in their head
while reading and masks intent. The intent of this code is to assign @data
with whatever is passed in, unless it’s a Hash
, if it’s a Hash
it’s supposed
to be a Wrapper
.
Let’s see if we can write this a little better to help bring that intent forward:
|
This has removed the double assignment and focused on the fact that @data
is
either going to be one or the other of something. One thing to note is this
has added lines of code to the definition, but I feel they are worth it.
Another way you could layout this method and slim it down would be like this:
|
I am not a big fan of the above examle; but some might love it… Here would be my best solution idea for the code currently:
|
This adds even more lines! However, it adds more description into what is going
on as well. @data
is being transformed, and if you want to see what that
means you can look at the method transform_format
to see exactly what that
means.