<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users937</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/937" rel="self"/>
  <title>kmcd</title>
  <updated>Wed Aug 13 12:46:14 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14959</id>
    <published>2008-08-13T12:46:14-07:00</published>
    <title>[Ruby] On Similar Methods Refactoring</title>
    <content type="html">&lt;p&gt;Most comments are in the code. Obviously I couldn't run this. &lt;/p&gt;

&lt;p&gt;I'm not sure if the job levels are stored as 'name'. If they are, you could eliminate the JOB_LEVELS hash. Less code === better code.&lt;/p&gt;

&lt;p&gt;Further refactoring of the extended boolean expression significantly reduces the readability IMHO.&lt;/p&gt;

&lt;pre&gt;JOB_LEVELS = {
  :apprentice   =&amp;gt; 1,
  :practicioner =&amp;gt; 2,
  :master       =&amp;gt; 3,
  :designer     =&amp;gt; 4,
  :engineer     =&amp;gt; 5,
  :architect    =&amp;gt; 6
}

# Usage: upgrade_to?(:apprentice)
# =&amp;gt; true
def upgrade_to?(level)
  rank = Rank.find_by_id JOB_LEVELS[level]
  
  # If/else not needed as this expression will evaluate to true/false
  self.cbc &amp;gt;= rank.cbc &amp;amp;&amp;amp; self.dbc &amp;gt;= rank.dbc &amp;amp;&amp;amp;
  self.cbp &amp;gt;= rank.cbp &amp;amp;&amp;amp; self.dbp &amp;gt;= rank.dbp &amp;amp;&amp;amp;
  self.cbs &amp;gt;= rank.cbs &amp;amp;&amp;amp; self.dbs &amp;gt;= rank.dbs &amp;amp;&amp;amp;
  self.cpd &amp;gt;= rank.cpd &amp;amp;&amp;amp; self.dpd &amp;gt;= rank.dpd &amp;amp;&amp;amp;
  self.cpe &amp;gt;= rank.cpe &amp;amp;&amp;amp; self.dpe &amp;gt;= rank.dpe &amp;amp;&amp;amp;       
  self.cpa &amp;gt;= rank.cpa &amp;amp;&amp;amp; self.dpa &amp;gt;= rank.dpa &amp;amp;&amp;amp;
  self.cnb &amp;gt;= rank.cnb &amp;amp;&amp;amp; self.dnb &amp;gt;= rank.dnb &amp;amp;&amp;amp;
  self.cad &amp;gt;= rank.cad &amp;amp;&amp;amp; self.dad &amp;gt;= rank.dad
end

# Further refactoring

# rank = Rank.find_by_id JOB_LEVELS[level]
# Could also be written as: (eliminating the need for JOB_LEVELS)
# rank = Rank.find_by_name level.to_s
# Assuming that the job level is stored in the 'name' column
  
# self.cbc &amp;gt;= rank.cbc &amp;amp;&amp;amp; self.dbc &amp;gt;= rank.dbc &amp;amp;&amp;amp; ...
# The extended boolean expression could be refactored to the following:
# (There is a reduction in readability here however.)
# vars = %w[ bc bp bs pd pe pa nb ad ].map { |var| [ &amp;quot;c&amp;quot; + var, &amp;quot;d&amp;quot; + var ]}.flatten
# 
# expr = vars.inject(&amp;quot;&amp;quot;) do |accum,var| 
#   accum = &amp;quot;self.#{var} &amp;gt;= rank.#{var} &amp;amp;&amp;amp;&amp;quot;
# end.chomp &amp;quot;&amp;amp;&amp;amp;&amp;quot;
# 
# eval(expr)&lt;/pre&gt;</content>
    <author>
      <name>kmcd</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/432-similar-methods-refactoring/refactors/14959" rel="alternate"/>
  </entry>
</feed>

