You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 4: Refactoring

Lab 4: Refactoring

Objectives

During this lab session:

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

The lab activities can be developed individually or in pairs.

The lab report must be developed individually.

  1. Create a folder called refactoring. Inside this folder, create two files called student.rb and test_student.rb.

    Both Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:

    # Lab 4: Refactoring
    # Date: 14-Sep-2011
    # Authors:
    # 456654  Anthony Stark 
    # 1160611 Thursday Rubinstein
  2. Place the following code in the file called student.rb:

    # File: student.rb
    
    class Student
    
      attr_reader :name, :id   
            
      def initialize(name, id, anual_income)
        @name = name 
        @id = id
        @anual_income = anual_income
        @grades = []        
      end
      
      def reset_anual_income(anual_income)
        previous_anual_income = @anual_income
        @anual_income = anual_income
        previous_anual_income
      end
      
      def add_grade(grade)
        @grades << grade
        self
      end
      
      def meh
        # Display Personal Information
        puts "Name: #{ @name } ID: #{ @id }"
        puts "Anual income: #{ @anual_income }"
        value = 0
        @grades.each do |grade|
          value += grade
        end
        value = value / @grades.size.to_f
        puts "Grade average: #{ value }"
            
        # Display Disclaimer
        puts 'The contents of this class must not be considered an offer,'
        puts 'proposal, understanding or agreement unless it is confirmed'
        puts 'in a document signed by at least five blood-sucking lawyers.'    
      end
      
      def scholarship_worthy?    
        # Nothing reasonable to do if this student has currently no grades. 
        return -1 if  @grades.empty?
        
        # A student is worthy of a scholarship if he/she has good grades and
        # is poor.
        value = 0
        @grades.each do |grade|
          value += grade
        end
        value = value / @grades.size.to_f
        (value >= 85) and (@anual_income < 15_000)    
      end      
      
    end

    In the test_student.rb file write a complete unit test class in order to thoroughly verify the current behavior of Student instances. Make sure to individually test the meh and scholarship_worthy? methods.

  3. Modify the Student class so that you carry out the following refactorings as many times as you consider necessary:

    1. Replace Magic Number with Symbolic Constant
    2. Rename Method
    3. Split Temporary Variable
    4. Extract Method
    5. Hide Method
    6. Introduce Explaining Variable
    7. Replace Error Code with Exception
    8. Introduce Named Parameter
    9. Separate Query from Modifier
    10. Replace Loop with Collection Closure Method

    Clearly state in your lab report how the code looked before and after the refactoring.

    Rerun (and adjust if necessary) your unit tests to make sure that the refactorings didn't modify the class' external behavior.

Deliverables

To hand in your individual lab work, follow these instructions.

Due date is Tuesday, September 20.

Evaluation

This activity will be evaluated using the following criteria:

50% Implementation of functional requirements.
50% Lab report.
DA The program and/or report was plagiarized.
© 1996-2011 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid HTML5 | Valid CSS