CSC/ECE 517 Fall 2014/OSS E1466 gjf: Difference between revisions
m (→Summary) |
m (→Summary) |
||
Line 40: | Line 40: | ||
*Used Object-Oriented Design patterns of Ruby: let the array respond to the method '''min''' which returns the minimum number in the array and get the logical penalty other than using if statement.(max_penalty if total_penalty is larger than max_penalty, total_penalty if total_penalty is smaller than max_penalty) | *Used Object-Oriented Design patterns of Ruby: let the array respond to the method '''min''' which returns the minimum number in the array and get the logical penalty other than using if statement.(max_penalty if total_penalty is larger than max_penalty, total_penalty if total_penalty is smaller than max_penalty) | ||
*Eliminated the unnecessary if | *Eliminated the unnecessary if statements to make the code clear to read. | ||
====Implementation==== | ====Implementation==== |
Revision as of 20:25, 26 October 2014
E1466: Refactoring GradesController
This wiki deals with our implementation of a controller in expertiza: grade_controller for the Expertiza Project using Ruby on Rails.
Introduction
Expertiza is a web application where students can submit and peer-review learning objects (articles, code, web sites, etc). It is used in select courses at NC State and by professors at several other colleges and universities.
Project Description
Classes involved: grades_controller.rb
What it does:
This class lists the grades of all the participants for an assignments and also their reviews. Instructor can edit scores, calculate penalties and send emails for conflicts.
What needs to be done:
- Modify calculate_all_penalties method which is too complex and long.
- Put the get_body_text method in a mailer rather than in the grades_controller.
- Refactor conflict_nofication method to conflict_email and make it delegated to the mailer.
- Refactor view_my_scores method to grades_show and delete the unnecessary variables.
- Try not to query for reviews and meta reviews in grades_show method.
Code Modification
calculate_all_penalties Method
Summary
We have modified the calculate_penalties method and implemented all the functions it originally has. After our modification the method shrinks from 45 lines to 32 lines of Ruby code. Originally, the method includes a lot of unnecessary if and unless statements which makes the method uneasy to read and seems to have a difficult logic to calculate the penalties.
According to the codeclimate which calculate the complexity of the method, originally, it has a complexity of 85, however, after we modified it, the complexity goes down to 65. Please check at original version,modified version
We have done the following to the original methods:
- Used for loop with selective choice to create penalty attributes and put it into the CalculatedPenalty Table, instead of duplicating the almost same code.
- Used Object-Oriented Design patterns of Ruby: let the array respond to the method min which returns the minimum number in the array and get the logical penalty other than using if statement.(max_penalty if total_penalty is larger than max_penalty, total_penalty if total_penalty is smaller than max_penalty)
- Eliminated the unnecessary if statements to make the code clear to read.
Implementation
- Modify if statements:
Before Refactoring:
if(penalties[:submission].nil?) penalties[:submission]=0 end if(penalties[:review].nil?) penalties[:review]=0 end if(penalties[:meta_review].nil?) penalties[:meta_review]=0 end
After Refactoring:
penalties[:submission] = 0 if penalties[:submission].nil? penalties[:review] = 0 if penalties[:review].nil? penalties[:meta_review] = 0 if penalties[:meta_review].nil?
- Using Objected-oriented Pattern
Before Refactoring:
if(@total_penalty > l_policy.max_penalty) @total_penalty = l_policy.max_penalty end
After Refactoring:
@total_penality=[l_policy.max_penalty,@total_penality].min
- Using loops rather than duplicate same code
Before Refactoring:
penalty_attr1 = {:deadline_type_id => 1,:participant_id => @participant.id, :penalty_points => penalties[:submission]} CalculatedPenalty.create(penalty_attr1) penalty_attr2 = {:deadline_type_id => 2,:participant_id => @participant.id, :penalty_points => penalties[:review]} CalculatedPenalty.create(penalty_attr2) penalty_attr3 = {:deadline_type_id => 5,:participant_id => @participant.id, :penalty_points => penalties[:meta_review]} CalculatedPenalty.create(penalty_attr3)" @all_penalties[participant.id] = Hash.new @all_penalties[participant.id][:submission] = penalties[:submission] @all_penalties[participant.id][:review] = penalties[:review] @all_penalties[participant.id][:meta_review] = penalties[:meta_review] @all_penalties[participant.id][:total_penalty] = @total_penalty"
After Refactoring:
deadline_type=[1,2,5] penalty_type=[:submission,:review,:meta_review] if calculate_for_participants for i in 0..2 penalty_attr={:deadline_type_id=>deadline_type[i],:participant_id => @participant.id, :penalty_points => penalties[penalty_type[i]]} CalculatedPenalty.create(penalty_attr) end end @all_penalties[participant.id] = {} for i in 0..2 @all_penalties[participant.id][:penalty_type[i]] = penalties[:penalty_type[i]] end @all_penalties[participant.id][:total_penalty] = @total_penalty"
conflict_email Method
Summary
Implementation
- specific reasons/ Design Pattern
Before Refactoring:
code here
After Refactoring:
code here
view_my_scores Method
Summary
The original view_my_scores method is too long and too slow, which includes many unnecessary instance variables along with several review and meta reviews methods inside it. Our job in this part is to change view_my_scores method into grades_show, refactor grades_show method so that the system will only search for scores in the database rather than search for scores and reviews, which wastes a lot of time. We also optimize get_scores method to improve the efficiency of showing scores by students and instructors.
Implementation
- specific reasons/ Design Pattern
Before Refactoring:
code here
After Refactoring:
code here