Software Development (CSC 321 2016S) : EBoards

CSC321.01 2016S, Class 14: Codes of Ethics


Overview

Preliminaries

Admin

Upcoming Work

Good Things to Do

Academic

Peer

Questions

Ugly Code, Revisited

class TimeSetter
  def self.convert(daysleft)
    year = 1980
    # Keep moving into the next year until we are down
    # to a reasonable number of days
    while (daysleft > 365) do
      daysleft -= 365
      # There's an extra day in a leap year!
      if (isleapyear year)
        daysleft -= 1
      end
      year += 1
    end
    return (year,daysleft)
  end

  def self.isleapyear(year)
    return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
  end
end

This is still wrong. Why?

Fix?

class TimeSetter
  def self.convert(days_since_start_of_time)
    daysleft = days_since_start_of_time
    year = 1980
    # Keep moving into the next year until we are down
    # to a reasonable number of days
    while (daysleft > 365) do
      daysleft -= 365
      # There's an extra day in a leap year!
      if (isleapyear year)
        daysleft -= 1
      end
      year += 1
    end
    return (year,daysleft)
  end

  def self.isleapyear(year)
    return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
  end
end

Approach one: Adding the code

    if daysleft > 0
      ++year
    else if (is_leap_year year)
      daysleft = 366
    else
      daysleft = 365
    end
class TimeSetter
  def self.convert(days_since_start_of_time)
    daysleft = days_since_start_of_time
    year = 1980
    # Keep moving into the next year until we are down
    # to a reasonable number of days
    while (daysleft > 365) do
      daysleft -= 365
      # There's an extra day in a leap year!
      if (isleapyear year)
        daysleft -= 1
      end
      if daysleft > 0
        ++year
      else if (is_leap_year year)
        daysleft = 366
      else
        daysleft = 365
      end
    end
    return (year,daysleft)
  end

  def self.isleapyear(year)
    return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
  end
end

Figure out a loop invariant (a statement about the status of the system that you can guarantee is true at the end of the loop if it's true at the beginning of the loop, and that is helpful in achieving the goals of the program.)

Another solution: In the while loop, do a more careful test

class TimeSetter
  def self.convert(days_since_start_of_time)
    daysleft = days_since_start_of_time
    year = 1980
    # Keep moving into the next year until we are down
    # to a reasonable number of days
    while (daysleft > 365) do
      if (is_leap_year year && daysleft >= 366)
        daysleft -= 366
        year += 1
      else if (is_leap_year year)
        break out of the loop;
      else
        daysleft -= 365
        year += 1
      end
    end
    return (year,daysleft)
  end

  def self.isleapyear(year)
    return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
  end
end
class TimeSetter
  def self.convert(days_since_start_of_time)
    daysleft = days_since_start_of_time
    year = 1980
    # Keep moving into the next year until we are down
    # to a reasonable number of days
    while (daysleft > (daysinyear year))
      daysleft -= (daysinyear year)
      year += 1
    end
    return (year,daysleft)
  end

  def self.daysinyear(year)
    if (isleapyear year)
      return 366
    else
      return 365
    end
  end

  def self.isleapyear(year)
    return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
  end
end

Professional Codes of Ethics

Why do we have professional codes of ethics?

Comments From Students

Agile and Ethical