TDD or not to be








Jakub Nieznalski

Is the code developed using Test-Drived Development better than the one using Test After technique?









What does is mean that the code is good or bad?


TDD Experiments in IBM and Microsoft
 D. S. Janzen
E. M. Laurie Williams


Conclusion 1: It does not matter if the code was written using TDD or Test After



Conclusion 2: The code that was written by developer with more years of experience in programming is better in terms of OO metrics

 

But is it really like that?

 def workdays_between (ts_from, ts_to)  wdays = (ts_to.to_i - ts_from.to_i) / 360.0 / 240
  if wdays > 1
    tsfd    = ts_from.to_date # midnight
    tstd    = ts_to.to_date
    days    = wdays.truncate
    weeks   = days  / 7
    wdays  -= weeks * 2
    wdays  += 1 if weeks > 0 && ts_to.cwday > 5
    days    = ts_to.cwday - ts_from.cwday
    days   += 7 if days < 0
    if days > 1
      from = tsfd
      (days - 1).times do |i|
        from  += 1.day
        wdays -= 1 if from < tstd && from.cwday > 5 # substract Sat or Sun inbetween
      end
    end
    # ALe 2013-10-19: changed boundaries to EXCLUDE ts_from and ts_to themself (if they SAY thei did it on a holiday, we believe them)
    Holidays.between(ts_from + 1.day, ts_to - 1.day, :de).each do |h| # VERY convenient, isn't it? Thanks, Alex Dunae
      wdays -= 1 unless h[:date].cwday > 5
    end
  end
  wdays < 0 ? 0 : wdays
end

 def weekdays(ts_from, ts_to)
  (ts_from...ts_to).select do |day|
    day.weekday? || day == ts_from || day == ts_to
  end.count
end

def day_part_difference ts_from, ts_to
   day_part(ts_to) - day_part(ts_from)
end

def day_part day
  day - day.midnight
end

def holidays(ts_from, ts_to)
  Holidays.between(ts_from.next_day, ts_to.prev_day,  :de).select{|day| day[:date].weekday? }.count
end

def workdays_between (ts_from, ts_to)
  weekdays(ts_from.to_date, ts_to.to_date) + day_part_difference(ts_from, ts_to) - holidays(ts_from, ts_to)
end

Think about interface


contracts = [c1, c2, c3]
 class Contract
    has_one :verification
calculate percentage of positive verification's


 def PercentageCalculator

  def calculate_percentage(contracts)
    contract.select do |c| 
      c.verification.positive == true
    end.count/contract.count
  end
end

Test first

 describe Calculator do
  describe "#calculate_percentage" do
    it "returns percentage of positives";
      Calculator.new(something).calculate_percentage
   end
end

class Calculator    def initialize(collection)
      @collection = collection
    end

  def calculate_percentage
    collection.select{|item| item.positive? }/ collection.size
  end
end

class Contract delegate :positive, :to => :verification ...
Calculator.new(contracts).calculate_percentage



   def PercentageCalculator
  def calculate_percentage(contracts)
    contract.select do |c| 
      c.verification.positive == true
    end.count/contract.count
  end
end


What do you think?

TDD or not to be

By Jakub Nieznalski

TDD or not to be

  • 818