CSC/ECE 517 Fall 2016/E1659. Refactor on the fly calc.rb
This wiki page is for the description of changes made under E1659 OSS assignment for Fall 2016, CSC/ECE 517.
Peer Review Information
For users intending to view the deployed Expertiza associated with this assignment, the credentials are below:
- Instructor login: username -> instructor6, password -> password
- Student login: username -> student5431, password -> password
- Student login: username -> student5427, password -> password
Expertiza Background
Expertiza is an educational web application created and maintained by the joint efforts of the students and the faculty at NCSU. It’s an open source project developed on Ruby on Rails platform and it’s code is available on Github. Using Expertiza students can bid for a particular project topic and the faculty assigns the same to different groups. Students can track their grades for previous projects and make new submissions for current projects. It also allows students to review each other’s work and improve their work upon this feedback.
Description of the current project
We worked on a module named on_the_fly_calc in this Expertiza based OSS project. We focussed on refactoring some methods which were very complex and also modified the language of the module to make it more ruby-istic. We also worked on refactoring the code to reduce redundancy. Our goal in this project was to make this model easier to read and maintain.
Files modified in current project
A model was modified for this project namely:
1. On_the_fly_calc
On_the_fly_calc
On_the_fly_calc is a module that is included in assignment.rb. What this module does is that it calculates the score for both students and instructors. E.g., when a student clicks “view my score”, Expertiza calculates the review scores for each review. This is called “on the fly” because expertiza stores the review scores based on each question (in answers table), but does not store the total score that a reviewer gives to a reviewee. It directly calculates the total review score for each review, using the score values in the answer table during run-time. Eg: For a set of 4 questions a reviewer gives 5 on 5 points for 2 questions and 4 on 5 points for other 2 questions. So only these points are stored in the database and when a reviewee clicks to see the score given by this particular reviewer, on_the_fly_calc module helps calculate the total score i.e. 90% in this case.
List of changes
The main aim was to reduce the complexity grade of ‘on_the_fly_calc.rb’ module from “F” to “C” in CodeClimate, which was achieved. We worked on the following work items(WIs) in order to refactor the module:
WI1 : Refactor ‘Similar Code’ in lines 34-42 and 62-70 in on_the_fly_calc.rb
WI2 : Prefer ‘each’ over every instance of ‘for loop’
WI3 : Refactor ‘scores’ method to reduce ABC (assignment, branch and condition) size
WI4 : Refactor ‘compute_reviews_hash’ method to reduce ABC (assignment, branch and condition) size
WI5 : Refactor ‘compute_avg_and_ranges_hash’ method to reduce ABC (assignment, branch and condition) size
Solutions Implemented and Delivered
Refactored similar code
Lines 34-42 and 62-70 were similar in compute_reviews_hash method. Hence, a dedicated method ‘calc_review_score’ was defined and this method was called in the both instances inside compute_reviews_hash.
Lines 34-42 in compute_reviews_hash:
Similar code in Lines 62-70:
Refactored method 'calc_review_score' for the two similar codes:
Each loop prefered over For loop
In every instance where ‘for’ loops were used in on_the_fly_calc module, ‘each…do’ was replaced instead. This significantly reduced the Cyclomatic complexity of the code.
The following screen shows the instances where for loops where replaced:
Refactored methods - 'scores', 'compute_reviews_hash', 'compute_avg_and_ranges_hash'
1. a) Method ‘scores’ was very complex, performing too many functions within a single method. Hence, every specific function was broken down into 4 separate smaller methods. This significantly reduced the Assignment, Branch, Condition size for ‘scores’ method.
b) The following 4 methods were created after splitting the first method
i. participant_score
ii. assess
iii. calculate_score
iv. calculate_assessment
c) Complex and long statements or loops were broken into simpler multiple statements.
2. a) We also refactored method ‘compute_reviews_hash’ which was very complex and modularized individual functionalities into 2 separate methods.
b) The following 2 methods were created and called after refactoring:
i. scores_varying_rubrics
ii. scores_non_varying_rubrics
3. Similarly, method ‘compute_avg_and_ranges_hash’ was also refactored and a seperate method calc_contri_score was defined to calculate contributor’s score. This method was then called from ‘compute_avg_and_ranges_hash’.