CSC/ECE 517 Fall 2014/oss E1456 akk: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(38 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== '''Refactoring Questionnaire Controller''' ==
== '''E1456 Refactoring Questionnaire Controller''' ==


__TOC__
__TOC__
[https://docs.google.com/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# Problem_Statement] <br>
[https://docs.google.com/document/d/1qQD7fcypFk77nq7Jx7ZNyCNpLyt1oXKaq5G-W7zkV3k/edit# Global Rules]


=Introduction=
=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.
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.<bR>


=Why is Refactoring Required=
<B><U>Why is Refactoring Required</u></B>
*It helps in making the code more understandable
*It makes the code more maintainable
*To remove repetition of code


=Project Overview=
=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==
==Classes involved==
Line 38: Line 44:


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.
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 .


{| class="wikitable"
{| class="wikitable"
Line 46: Line 56:
! style="width:43%;"|Reason For Change
! style="width:43%;"|Reason For Change
|- style="vertical-align:top;"
|- style="vertical-align:top;"
}
|''''' 1 '''''
 
|delete
=Questionnaire Controller: questionnaire_controller.rb =
|redirect_to :action => 'list', :controller => 'tree_display' to redirect_to action: 'list', controller: 'tree_display'
 
|Refactoring using Global rules
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 .
|-
|''''' 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=
=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>
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
</pre>


After  
<b>After</b>
[[File:after_refactoring_function_name_change.PNG|frame|center]]
[[File:after_refactoring_function_name_change.PNG|frame|center]]


3. Global rule changes for using Hash - value key pair
3. <b>edit_advice method is evidently not used, and removed. This edit function is already implemented in advice_controller.rb. </b><br>
 
4. <b>save_advice has been moved to the advice_controller. </b><br>
 
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 78: Line 165:


<pre>
<pre>
Before Refactoring :-
<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
.......
</pre>
 
<pre>
<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'
........
</pre>
 
<pre>
<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'
.......
</pre>
 
<pre>
<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'
.........
</pre>
 
<pre>
<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'
........
</pre>
 
<pre>
<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)
.........
</pre>
 
<pre>
<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
</pre>
 
<pre>
<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"
........
</pre>
 
<pre>
<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'
..........
</pre>
 
6. <b>".eql? nil" used instead of " == nil".</b>
<pre>
<b>Before Refactoring</b> :-
def edit
def edit
.........
.........
Line 88: Line 330:
..........
..........


After Refactoring :-
<b>After Refactoring</b> :-
def edit
def edit
..........
..........
Line 97: Line 339:
redirect_to controller: 'advice',  action: 'edit_advice',  id: params[:questionnaire][:id]
redirect_to controller: 'advice',  action: 'edit_advice',  id: params[:questionnaire][:id]
..........
..........
</pre>
</pre>


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 113: 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 124: Line 366:
</pre>
</pre>


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 141: 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 157: Line 400:
       end
       end
</pre>
</pre>
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 create_questionnaire
def save
.......
.......
redirect_to :controller => 'submitted_content', :action => 'edit', :id => participant_id
save_questions @questionnaire.id if @questionnaire.id != nil and @questionnaire.id > 0
.......
.......
 
QuestionnaireNode.create(:parent_id => parent.id, :node_object_id => @questionnaire.id)
After Refactoring Global rules :-
def create_questionnaire
.......
redirect_to controller: 'submitted_content', action: 'edit', id: participant_id
.......
.......


</pre>
<b>After refactoring Global rules </b>:-
 
def save
<pre>
Before Refactoring Global rules :-
 
def create_questionnaire
.......
.......
redirect_to :controller => 'tree_display', :action => 'list'
save_questions @questionnaire.id if @questionnaire.id != nil && @questionnaire.id > 0
.......
.......
 
QuestionnaireNode.create(parent_id: parent.id, node_object_id: @questionnaire.id)
After Refactoring Global rules :-
 
def create_questionnaire
.......
.......
redirect_to controller: 'tree_display', action: 'list'
........
</pre>
</pre>


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 create
..........
.......
if params[:new_question] and params[:new_choices]
redirect_to :controller => 'tree_display', :action => 'list'
...........
.......
print "=====choice_key="+choice_key+"======="
 
..........
After Refactoring Global rules :-
q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "true",:question_id => question.id)
 
.........
def create
q = QuizQuestionChoice.new(:txt => params[:new_choices][questionnum.to_s][q_type][choice_key][:txt], :iscorrect => "false",:question_id => question.id)
.......
.........
redirect_to controller: 'tree_display', action: 'list'
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)
</pre>
</pre>


<pre>
=Object Oriented Design Principles Followed=
Before refactoring Global rules :-


def update
The following object oriented design principles were followed during refactoring: <br>
........
*<b>Single Responsibility Principle</b>: 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.
redirect_to :controller => 'tree_display', :action => 'list'
*<b>DRY Principle</b>: Don't Repeat Yourself! The repetitive and redundant code was removed from the associated classes and methods.
........


After Refactoring Global rules :-
=Running Project & Testing Functionality=
We tested the functionality of the questionnaires controller before and after refactoring by manually creating and listing the questionnaires, there is no change in behavior observed. Please find the snapshots below for reference.
After Refactoring, the questionnaire controller is working as before.
[[File:rsz_questionnaires_index1.jpg|100px|frame|center]]


def update
........
redirect_to controller: 'tree_display', action: 'list'
.........


</pre>


<pre>
[[File:rsz_questionnaires_review.jpg|frame|center]]
Before Refactoring Global rules :-
def toggle_access
........
redirect_to :controller => 'tree_display', :action => 'list'
end


After Refactoring global rules :-
def toggle_access
........
redirect_to controller: 'tree_display', action: 'list'
........


</pre>


<pre>
[[File:rsz_questionnaires_teammate_review.jpg|frame|center]]
Before Refactoring Global rules :-
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)
.......
 
After refactoring Global rules :-
def save
.......
save_questions @questionnaire.id if @questionnaire.id != nil && @questionnaire.id > 0
.......
QuestionnaireNode.create(parent_id: parent.id, node_object_id: @questionnaire.id)
.......
</pre>


=Conclusion=
=Conclusion=
Line 266: Line 490:
[http://en.wikipedia.org/wiki/Code_refactoring Code Refactoring Wiki]<br>
[http://en.wikipedia.org/wiki/Code_refactoring Code Refactoring Wiki]<br>
[https://docs.google.com/document/d/1qQD7fcypFk77nq7Jx7ZNyCNpLyt1oXKaq5G-W7zkV3k/edit Global Rules for Refactoring]<br>
[https://docs.google.com/document/d/1qQD7fcypFk77nq7Jx7ZNyCNpLyt1oXKaq5G-W7zkV3k/edit Global Rules for Refactoring]<br>
[http://www.agiledeveloper.com/presentations/how_to_approach_refactoring_2up.pdf Why Refactor]
[http://www.agiledeveloper.com/presentations/how_to_approach_refactoring_2up.pdf Why Refactor]<br>
[http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]<br>

Latest revision as of 03:33, 30 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

GitHub Project Repository

VCL IP Address

Expertiza Wiki

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.

Running Project & Testing Functionality

We tested the functionality of the questionnaires controller before and after refactoring by manually creating and listing the questionnaires, there is no change in behavior observed. Please find the snapshots below for reference. After Refactoring, the questionnaire controller is working as before.



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