Software Development (CSC 321 2015F) : EBoards

CSC321.01 2015F, Class 08: Test-Driven Development and Code smells


Overview

Preliminaries

Admin

Review: Buggy code

class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 365) do
      if ((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
        if (d > 366)
          d -= 366
          y += 1
        end
      else
        d -= 365
        y += 1
      end
    end
    return [y,d]
  end
end

What does this program try to do?

What is wrong with this program?

How would we find it?

How would we fix it?

class TimeSetter
  def self.isLeapYear(year)
    ((year % 400) == 0) || ((year % 4 == 0) && (year % 100 != 0))
  end

  def self.daysInYear(year)
    if isLeapYear(year)
      366
    else
      365
    end
  end

  def self.convert(d)
    y = 1980;
    while (d > daysInYear(y))
      d -= daysInYear(y)
      y += 1
    end
    return [y,d]
  end
end

Loop invariants

A technique for developing correct loop code and think through what your code does.

Three parts

Note:

As we write any algorithm, it's useful to know what all the variables represent.

Code smells

Test-driven development

Pure TDD

Reality

Code coverage

Work time