CSC/ECE 517 Fall 2014/oss E1456 akk: Difference between revisions
Line 124: | Line 124: | ||
=Code Snippets= | =Code Snippets= | ||
1. The quiz questionnaire related methods such as view_quiz, update_quiz, new_quiz and validate_quiz are moved to questionnaire_helper.rb to make the questionnaires_controller thin and follow global rule of thin controllers and fat models.<br> | 1. <b>The quiz questionnaire related methods such as view_quiz, update_quiz, new_quiz and validate_quiz are moved to questionnaire_helper.rb to make the questionnaires_controller thin and follow global rule of thin controllers and fat models</b>.<br> | ||
[[File:add_include_line.PNG|frame|center]] | [[File:add_include_line.PNG|frame|center]] | ||
2. Non Restful method names changed accordingly to make them Restful. <br> | 2. <b>Non Restful method names changed accordingly to make them Restful.</b> <br> | ||
Before | <b>Before<b> | ||
<pre> | <pre> | ||
Line 143: | Line 143: | ||
</pre> | </pre> | ||
After | <b>After</b> | ||
[[File:after_refactoring_function_name_change.PNG|frame|center]] | [[File:after_refactoring_function_name_change.PNG|frame|center]] | ||
3. edit_advice method is evidently not used, and removed. This edit function is already implemented in advice_controller.rb. <br> | 3. <b>edit_advice method is evidently not used, and removed. This edit function is already implemented in advice_controller.rb. </b><br> | ||
4. save_advice has been moved to the advice_controller. <br> | 4. <b>save_advice has been moved to the advice_controller. </b><br> | ||
5. Global rule changes for using Hash - value key pair implemented [Use key: ‘value’, not :key => ‘value’] accordingly. | 5. <b>Global rule changes for using Hash - value key pair implemented [Use key: ‘value’, not :key => ‘value’] accordingly.</b> | ||
<pre> | <pre> | ||
Before Refactoring :- | <b>Before Refactoring</b> :- | ||
def delete | def delete | ||
......... | ......... | ||
redirect_to :action => 'list', :controller => 'tree_display' | redirect_to :action => 'list', :controller => 'tree_display' | ||
......... | ......... | ||
After Refactoring :- | <b>After Refactoring</b> :- | ||
def delete | def delete | ||
......... | ......... | ||
Line 165: | Line 165: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
....... | ....... | ||
Line 171: | Line 171: | ||
....... | ....... | ||
After Refactoring Global rules :- | <b>After Refactoring Global rules</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
....... | ....... | ||
Line 179: | Line 179: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
Line 186: | Line 186: | ||
....... | ....... | ||
After Refactoring Global rules :- | <b>After Refactoring Global rules</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
Line 195: | Line 195: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def create | def create | ||
Line 202: | Line 202: | ||
....... | ....... | ||
After Refactoring Global rules :- | <b>After Refactoring Global rules</b> :- | ||
def create | def create | ||
Line 211: | Line 211: | ||
<pre> | <pre> | ||
Before refactoring Global rules :- | <b>Before refactoring Global rules</b> :- | ||
def update | def update | ||
Line 218: | Line 218: | ||
........ | ........ | ||
After Refactoring Global rules :- | <b>After Refactoring Global rules</b> :- | ||
def update | def update | ||
Line 227: | Line 227: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def toggle_access | def toggle_access | ||
........ | ........ | ||
Line 233: | Line 233: | ||
end | end | ||
After Refactoring global rules :- | <b>After Refactoring global rules</b> :- | ||
def toggle_access | def toggle_access | ||
........ | ........ | ||
Line 241: | Line 241: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def save_new_questions(questionnaire_id) | def save_new_questions(questionnaire_id) | ||
......... | ......... | ||
Line 247: | Line 247: | ||
........ | ........ | ||
After refactoring Global rules :- | <b>After refactoring Global rules</b> :- | ||
def save_new_questions(questionnaire_id) | def save_new_questions(questionnaire_id) | ||
........ | ........ | ||
Line 255: | Line 255: | ||
<pre> | <pre> | ||
Before Refactoring Extra print statement and deletion of an extra statement:- | <b>Before Refactoring Extra print statement and deletion of an extra statement</b>:- | ||
def save_questions(questionnaire_id) | def save_questions(questionnaire_id) | ||
........... | ........... | ||
Line 266: | Line 266: | ||
end | end | ||
After Refactoring Extra print statement and deletion of an extra statement:- | <b>After Refactoring Extra print statement and deletion of an extra statement</b>:- | ||
def save_questions(questionnaire_id) | def save_questions(questionnaire_id) | ||
........... | ........... | ||
Line 277: | Line 277: | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules</b> :- | ||
def export | def export | ||
......... | ......... | ||
Line 284: | Line 284: | ||
.......... | .......... | ||
After Refactoring Global rules :- | <b>After Refactoring Global rules</b> :- | ||
def export | def export | ||
......... | ......... | ||
Line 293: | Line 293: | ||
<pre> | <pre> | ||
Before Refactoring Global rules + Function name change + statement deletion :- | <b>Before Refactoring Global rules + Function name change + statement deletion</b> :- | ||
def clone_questionnaire_details(questions) | def clone_questionnaire_details(questions) | ||
......... | ......... | ||
Line 305: | Line 305: | ||
......... | ......... | ||
After Refactoring Global rules + Function name change + statement deletion :- | <b>After Refactoring Global rules + Function name change + statement deletion</b> :- | ||
def clone_questionnaire(questions) | def clone_questionnaire(questions) | ||
......... | ......... | ||
Line 318: | Line 318: | ||
</pre> | </pre> | ||
6. ".eql? nil" used instead of " == nil". | 6. <b>".eql? nil" used instead of " == nil".</b> | ||
<pre> | <pre> | ||
Before Refactoring :- | <b>Before Refactoring</b> :- | ||
def edit | def edit | ||
......... | ......... | ||
Line 330: | Line 330: | ||
.......... | .......... | ||
After Refactoring :- | <b>After Refactoring</b> :- | ||
def edit | def edit | ||
.......... | .......... | ||
Line 341: | Line 341: | ||
</pre> | </pre> | ||
7. Removed duplicate and redundant code making the code DRY. | 7. <b>Removed duplicate and redundant code making the code DRY.</b> | ||
<pre> | <pre> | ||
Before Refactoring Redundant If in def create_questionnaire :- | <b>Before Refactoring Redundant If in def create_questionnaire</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
........ | ........ | ||
Line 355: | Line 355: | ||
end | end | ||
After Refactoring Redundant If in def create_questionnaire :- | <b>After Refactoring Redundant If in def create_questionnaire</b> :- | ||
def create_questionnaire | def create_questionnaire | ||
........ | ........ | ||
Line 366: | Line 366: | ||
</pre> | </pre> | ||
8. Removed unnecessary debug prints present in the code. | 8. <b>Removed unnecessary debug prints present in the code.</b> | ||
<pre> | <pre> | ||
Before Refactoring Un-necessary Print statements in def create_questionnaire :- | <b>Before Refactoring Un-necessary Print statements in def create_questionnaire </b>:- | ||
def create_questionnaire | def create_questionnaire | ||
Line 384: | Line 384: | ||
end | end | ||
After Refactoring Un-necessary Print statements in def create_questionnaire :- | <b>After Refactoring Un-necessary Print statements in def create_questionnaire </b>:- | ||
def create_questionnaire | def create_questionnaire | ||
Line 401: | Line 401: | ||
</pre> | </pre> | ||
9. Used `&&` and `||` rather than `and` and `or` to keep boolean precedence as per global rules. | 9. <b>Used `&&` and `||` rather than `and` and `or` to keep boolean precedence as per global rules.</b> | ||
<pre> | <pre> | ||
Before Refactoring Global rules :- | <b>Before Refactoring Global rules </b>:- | ||
def save | def save | ||
....... | ....... | ||
Line 412: | Line 412: | ||
....... | ....... | ||
After refactoring Global rules :- | <b>After refactoring Global rules </b>:- | ||
def save | def save | ||
....... | ....... | ||
Line 421: | Line 421: | ||
</pre> | </pre> | ||
10. More global rule changes. | 10. <b>More global rule changes.</b> | ||
<pre> | <pre> | ||
Before Refactoring Global Rules :- | <b>Before Refactoring Global Rules </b>:- | ||
def save_choices(questionnaire_id) | def save_choices(questionnaire_id) | ||
.......... | .......... | ||
Line 442: | Line 442: | ||
q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "false",:question_id => question.id) | q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "false",:question_id => question.id) | ||
After Refactoring Global Rules :- | <b>After Refactoring Global Rules </b>:- | ||
def save_choices(questionnaire_id) | def save_choices(questionnaire_id) | ||
.......... | .......... |
Revision as of 23:52, 29 October 2014
E1456 Refactoring Questionnaire Controller
Problem_Statement
Global Rules
Introduction
Questionnaire Controller interacts with the user to create and edit questionnaires such as review rubrics, teammate-feedback rubrics, quizzes, and surveys. This page provides a detailed description of Open Source Project on Expertiza for refactoring the questionnaire controller.
Why is Refactoring Required
- It helps in making the code more understandable
- It makes the code more maintainable
- To remove repetition of code
Project Overview
The scope of the project is to refactor Questionnaire Controller which is very huge, in order to follow the standard principle of Fat Models and slim controllers.
Classes involved
- questionnaire_controller.rb
- questionnaire_helper.rb
- advice_controller.rb
Changes made
- Functionality moved to quiz_questionnaire.rb.
- edit_advice method was not being used, so it was removed.
- save_advice moved to the advice_controller.
- copy, update_quiz, valid_quiz methods were long and have been broken up. clone_questionnaire_details was also broken up and renamed.
- Added comments to select_questionnaire_type
- Debug output (print statements) have been removed.
- Changed code to follow the global rules.
- save_new_questions, delete_questions, save_questions have been moved to a separate class.
Project Resources
UML CLass Diagram
A Subset of the UML class diagram including only the related classes for this project can be drawn as shown below.
Questionnaire Controller is the superclass of QuizQuestionnaire and all the other classes - User class, Advice class and Questionnaire classes are related model Classes to the controllers that we have modified.
Refactoring questionnaire_controller.rb
Several methods in Questionnaire Controller class required refactoring to ensure that the change is reflected in the entire project. The names were changed to follow method naming conventions in Ruby. Deprecated code was removed and re-coded to ensure that the code worked in future version of Rails as well. Duplicate codes were commented out. Methods that are more general in the sub classes were moved to the parent class. Some methods that calculate that were colliding with other files were moved to a helper method questionnaire_helper.rb .
Sr. No. | Method Name | Changes Made | Reason For Change |
---|---|---|---|
1 | delete | redirect_to :action => 'list', :controller => 'tree_display' to redirect_to action: 'list', controller: 'tree_display' | Refactoring using Global rules |
2 | edit | Three functions changed in this method | Refactoring using Global rules |
3 | create_questionnaire | Removal of redundant If statement | Removal of unnecessary statements |
4 | create_questionnaire | Commenting out unnecessary print statements | Refactoring of the function |
5 | create | statement change from redirect_to :controller => 'tree_display', :action => 'list' to redirect_to controller: 'tree_display', action: 'list' | Refactoring using Global rules |
6 | update | change from redirect_to :controller => 'tree_display', :action => 'list' to redirect_to controller: 'tree_display', action: 'list' | Refactoring using Global rules |
7 | toggle_access | change from redirect_to :controller => 'tree_display', :action => 'list' to redirect_to controller: 'tree_display', action: 'list' | Refactoring using Global rules |
8 | save | change from QuestionnaireNode.create(:parent_id => parent.id, :node_object_id => @questionnaire.id) to QuestionnaireNode.create(parent_id: parent.id, node_object_id: @questionnaire.id) | Refactoring using Global rules |
9 | save_new_questions(questionnaire_id) | change from a = QuestionAdvice.new(:score => i, :advice => nil) to a = QuestionAdvice.new(score: i, advice: nil) | Refactoring using Global rules |
10 | save_questions(questionnaire_id) | Deletion of an extra If statement and deletion of an unnecessary print statement | Refactoring was required for this function |
11 | save_choices(questionnaire_id) | Removal of unnecessary print statements and refactoring of the function | Refactoring done following Global rules and use of DRY principle |
12 | export | Refactoring done for 2 statements | Refactoring done using Global rules |
13 | clone_questionnaire_details(questions) | Refactoring of 4 statements done in this function | Refactoring was required for this function |
Code Snippets
1. The quiz questionnaire related methods such as view_quiz, update_quiz, new_quiz and validate_quiz are moved to questionnaire_helper.rb to make the questionnaires_controller thin and follow global rule of thin controllers and fat models.
2. Non Restful method names changed accordingly to make them Restful.
Before
def select_questionnaire_type @questionnaire = Object.const_get(params[:questionnaire][:type]).new(params[:questionnaire]) @questionnaire.private = params[:questionnaire][:private] @questionnaire.min_question_score = params[:questionnaire][:min_question_score] @questionnaire.max_question_score = params[:questionnaire][:max_question_score] @questionnaire.section = params[:questionnaire][:section] @questionnaire.id = params[:questionnaire][:id] @questionnaire.display_type = params[:questionnaire][:display_type] end
After
3. edit_advice method is evidently not used, and removed. This edit function is already implemented in advice_controller.rb.
4. save_advice has been moved to the advice_controller.
5. Global rule changes for using Hash - value key pair implemented [Use key: ‘value’, not :key => ‘value’] accordingly.
<b>Before Refactoring</b> :- def delete ......... redirect_to :action => 'list', :controller => 'tree_display' ......... <b>After Refactoring</b> :- def delete ......... redirect_to action: 'list', controller: 'tree_display' .........
<b>Before Refactoring Global rules</b> :- def create_questionnaire ....... redirect_to :controller => 'submitted_content', :action => 'edit', :id => participant_id ....... <b>After Refactoring Global rules</b> :- def create_questionnaire ....... redirect_to controller: 'submitted_content', action: 'edit', id: participant_id .......
<b>Before Refactoring Global rules</b> :- def create_questionnaire ....... redirect_to :controller => 'tree_display', :action => 'list' ....... <b>After Refactoring Global rules</b> :- def create_questionnaire ....... redirect_to controller: 'tree_display', action: 'list' ........
<b>Before Refactoring Global rules</b> :- def create ....... redirect_to :controller => 'tree_display', :action => 'list' ....... <b>After Refactoring Global rules</b> :- def create ....... redirect_to controller: 'tree_display', action: 'list' .......
<b>Before refactoring Global rules</b> :- def update ........ redirect_to :controller => 'tree_display', :action => 'list' ........ <b>After Refactoring Global rules</b> :- def update ........ redirect_to controller: 'tree_display', action: 'list' .........
<b>Before Refactoring Global rules</b> :- def toggle_access ........ redirect_to :controller => 'tree_display', :action => 'list' end <b>After Refactoring global rules</b> :- def toggle_access ........ redirect_to controller: 'tree_display', action: 'list' ........
<b>Before Refactoring Global rules</b> :- def save_new_questions(questionnaire_id) ......... a = QuestionAdvice.new(:score => i, :advice => nil) ........ <b>After refactoring Global rules</b> :- def save_new_questions(questionnaire_id) ........ a = QuestionAdvice.new(score: i, advice: nil) .........
<b>Before Refactoring Extra print statement and deletion of an extra statement</b>:- def save_questions(questionnaire_id) ........... print question_key .......... if (@questionnaire.type == "QuizQuestionnaire") Question.update(question_key,:weight => 1, :txt => params[:question][question_key][:txt] ) else Question.update(question_key, params[:question][question_key]) end <b>After Refactoring Extra print statement and deletion of an extra statement</b>:- def save_questions(questionnaire_id) ........... # print question_key .......... if (@questionnaire.type == "QuizQuestionnaire") Question.update(question_key, weight: 1, txt: params[:question][question_key][:txt] ) end
<b>Before Refactoring Global rules</b> :- def export ......... :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=questionnaires.csv" .......... <b>After Refactoring Global rules</b> :- def export ......... type: 'text/csv; charset=iso-8859-1; header=present', disposition: "attachment; filename=questionnaires.csv" ........
<b>Before Refactoring Global rules + Function name change + statement deletion</b> :- def clone_questionnaire_details(questions) ......... @questionnaire.name = 'Copy of '+orig_questionnaire.name ......... QuestionnaireNode.create(:parent_id => parent.id, :node_object_id => @questionnaire.id) ......... redirect_to :controller => 'questionnaire', :action => 'view', :id => @questionnaire.id ......... redirect_to :action => 'list', :controller => 'tree_display' ......... <b>After Refactoring Global rules + Function name change + statement deletion</b> :- def clone_questionnaire(questions) ......... #Statement deleted ......... QuestionnaireNode.create(parent_id: parent.id, node_object_id: @questionnaire.id) .......... redirect_to controller: 'questionnaire', action: 'view', id: @questionnaire.id .......... redirect_to action: 'list', controller: 'tree_display' ..........
6. ".eql? nil" used instead of " == nil".
<b>Before Refactoring</b> :- def edit ......... redirect_to Questionnaire if @questionnaire == nil ......... redirect_to :action => 'view', :id => @questionnaire .......... redirect_to :controller => 'advice', :action => 'edit_advice', :id => params[:questionnaire][:id] .......... <b>After Refactoring</b> :- def edit .......... redirect_to Questionnaire if @questionnaire.eql? nil .......... redirect_to action: 'view', id: @questionnaire ........... redirect_to controller: 'advice', action: 'edit_advice', id: params[:questionnaire][:id] ..........
7. Removed duplicate and redundant code making the code DRY.
<b>Before Refactoring Redundant If in def create_questionnaire</b> :- def create_questionnaire ........ if Team.find(t.team_id, @assignment.id) if team = Team.find(t.team_id, @assignment.id) break end end ..... end <b>After Refactoring Redundant If in def create_questionnaire</b> :- def create_questionnaire ........ if Team.find(t.team_id, @assignment.id) #Removed unneeded If statement team = Team.find(t.team_id, @assignment.id) break end end
8. Removed unnecessary debug prints present in the code.
<b>Before Refactoring Un-necessary Print statements in def create_questionnaire </b>:- def create_questionnaire ....... print "=====create_questionnaire=========" ..... @successful_create = true print "=====save in create_questionnaire begin=========" save print "=====save in create_questionnaire over=========" save_choices @questionnaire.id print "=====save_choice in create_questionnaire over=========" if @successful_create == true flash[:note] = "Quiz was successfully created" end <b>After Refactoring Un-necessary Print statements in def create_questionnaire </b>:- def create_questionnaire ....... # print "=====create_questionnaire=========" ....... @successful_create = true # print "=====save in create_questionnaire begin=========" save # print "=====save in create_questionnaire over=========" save_choices @questionnaire.id # print "=====save_choice in create_questionnaire over=========" if @successful_create == true flash[:note] = "Quiz was successfully created" end
9. Used `&&` and `||` rather than `and` and `or` to keep boolean precedence as per global rules.
<b>Before Refactoring Global rules </b>:- def save ....... save_questions @questionnaire.id if @questionnaire.id != nil and @questionnaire.id > 0 ....... QuestionnaireNode.create(:parent_id => parent.id, :node_object_id => @questionnaire.id) ....... <b>After refactoring Global rules </b>:- def save ....... save_questions @questionnaire.id if @questionnaire.id != nil && @questionnaire.id > 0 ....... QuestionnaireNode.create(parent_id: parent.id, node_object_id: @questionnaire.id) .......
10. More global rule changes.
<b>Before Refactoring Global Rules </b>:- def save_choices(questionnaire_id) .......... if params[:new_question] and params[:new_choices] ........... print "=====choice_key="+choice_key+"=======" .......... q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "true",:question_id => question.id) ......... q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "false",:question_id => question.id) ......... q = QuizQuestionChoice.new(:txt => "True", :iscorrect => "true",:question_id => question.id) ......... q = QuizQuestionChoice.new(:txt => "False", :iscorrect => "false",:question_id => question.id) ......... q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "true",:question_id => question.id) .......... q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "false",:question_id => question.id) <b>After Refactoring Global Rules </b>:- def save_choices(questionnaire_id) .......... if params[:new_question] && params[:new_choices] .......... # print "=====choice_key="+choice_key+"=======" .......... q = QuizQuestionChoice.new(txt: params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], iscorrect: "true", question_id: question.id) .......... q = QuizQuestionChoice.new(txt: params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], iscorrect: "false", question_id: question.id) .......... q = QuizQuestionChoice.new(txt: "True", iscorrect: "true", question_id: question.id) .......... q = QuizQuestionChoice.new(txt: "False", iscorrect: "false", question_id: question.id) ......... q = QuizQuestionChoice.new(txt: params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], iscorrect: "true", question_id: question.id) ......... q = QuizQuestionChoice.new(txt: params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], iscorrect: "false", question_id: question.id)
Object Oriented Design Principles Followed
The following object oriented design principles were followed during refactoring:
- Single Responsibility Principle: The single responsibility principle states that every context (class, function, variable, etc.) should have a single responsibility. It was maintained that each method should be involved with a single responsibility and the code that was not related to that particular functionality was moved to other method. This was also taken care of in terms of classes.
- DRY Principle: Don't Repeat Yourself! The repetitive and redundant code was removed from the associated classes and methods.
Conclusion
- Refactoring was performed as per requirements in the files questionnaire_controller.rb, and advice_controller.rb.
- The code from models cannot be moved directly to controllers, so the code was moved to the helper class questionnaire_helper.rb.
- Some methods with confusing method names have also been renamed.
References
Expertiza
Code Refactoring Wiki
Global Rules for Refactoring
Why Refactor
Single Responsibility Principle