Você está na página 1de 12

DRY

Don’t Repeat Yourself

Friday, December 17, 2010 1


The 4 Rules of Simple
Design
• Runs all the tests
• No duplication
• Expresses developer intent
• Minimizes the number of classes and
methods

Friday, December 17, 2010 2


The 4 Rules of Simple
Design

• No duplication = DRY

Friday, December 17, 2010 3


Easiest way to boost
your coding skills

Friday, December 17, 2010 4


Don’t take my word

Friday, December 17, 2010 5


Spot the Duplication

Friday, December 17, 2010 6


Plain Old Duplication
      if @unparsed_rubygems_output_flags
        flags = @unparsed_rubygems_output_flags.split(',')
        flags.delete_if {|flag| flag == nil or flag == ''}
        flags.map! {|flag| flag.downcase}
        flags.sort!
        flags.uniq!
        flags.map! {|flag| flag.to_sym}
        rubygems_output_valid = true
        flags.each do |flag|
          unless VALID_RUBYGEMS_OUTPUT_FLAGS.include?(flag)
            @output = "Invalid rubygems-output flag: #{flag}\n"
            rubygems_output_valid = false
          end
        end
        @options[:rubygems_output] = flags if rubygems_output_valid
      end

      if @unparsed_geminstaller_output_flags
        flags = @unparsed_geminstaller_output_flags.split(',')
        flags.delete_if {|flag| flag == nil or flag == ''}
        flags.map! {|flag| flag.downcase}
        flags.sort!
        flags.uniq!
        flags.map! {|flag| flag.to_sym}
        geminstaller_output_valid = true
        flags.each do |flag|
          unless VALID_GEMINSTALLER_OUTPUT_FLAGS.include?(flag)
            @output = "Invalid geminstaller-output flag: #{flag}\n"
            geminstaller_output_valid = false
          end
        end
        @options[:geminstaller_output] = flags if geminstaller_output_valid
      end

Friday, December 17, 2010 7


Intention Duplication
if (list.isEmpty()) {
....
}

....

if (list.size() == 0) {
....
}

Friday, December 17, 2010 8


Name Duplication

TestRunner.runTests()

def handle_web_response(web_response)

ProductSales productSales = new ProductSales()

Friday, December 17, 2010 9


Comment Duplication

// Restart the process


process.restart()

Friday, December 17, 2010 10


Structure Duplication

if isinstance(StringHandler, o):
o.updateString(context)
elif isinstance(IntHandler, o):
o.updateInt(context)

Friday, December 17, 2010 11


Let’s get it on!

• The Serializer Kata http://bit.ly/KataDRY


• Do not move to next step until code
duplication ≤ 0

Friday, December 17, 2010 12

Você também pode gostar