<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ajoshi5</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ajoshi5"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Ajoshi5"/>
	<updated>2026-06-08T23:32:39Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91465</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91465"/>
		<updated>2014-11-06T02:46:15Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Code Cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt; consists of methods involved in creating, scoring &amp;amp; recording responses of the quizzes taken by reviewers or students of the other teams with the same topic. &lt;br /&gt;
&amp;lt;br&amp;gt;The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow principle of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view. This is illustrated well in the example mentioned above for the [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Fall_2014/oss_E1460_aua#RESTful_style_implementation RESTful style implementation].&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database. By doing this, we followed the &amp;lt;b&amp;gt;Single Responsibility O-O Design Principle for methods&amp;lt;/b&amp;gt; mentioned by &amp;lt;b&amp;gt; Mr. Robert C. Martin &amp;lt;/b&amp;gt; which states that &amp;quot;Every software module should have only one reason to change&amp;quot;&amp;lt;ref&amp;gt;[http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles O-O Design Principles]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    questionnaire = Questionnaire.find(map.reviewed_object_id)&lt;br /&gt;
    scores = Array.new&lt;br /&gt;
    valid = true&lt;br /&gt;
    questions = Question.where(questionnaire_id: questionnaire.id)&lt;br /&gt;
    questions.each do |question|&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
      ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
      if ques_type.eql? 'MCC'&lt;br /&gt;
        if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        else&lt;br /&gt;
          params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
            correct_answers.each do |correct|&lt;br /&gt;
              if choice.eql? correct.txt&lt;br /&gt;
                score += 1&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
            end&lt;br /&gt;
            new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
            unless new_score.valid?&lt;br /&gt;
              valid = false&lt;br /&gt;
            end&lt;br /&gt;
            scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
            score = 1&lt;br /&gt;
          else&lt;br /&gt;
            score = 0&lt;br /&gt;
          end&lt;br /&gt;
          scores.each do |score_update|&lt;br /&gt;
            score_update.score = score&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = correct_answers.first&lt;br /&gt;
        if ques_type.eql? 'Essay'&lt;br /&gt;
          score = -1&lt;br /&gt;
        elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; response.id, :score =&amp;gt; score&lt;br /&gt;
        if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        end&lt;br /&gt;
        scores.push(new_score)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    if valid&lt;br /&gt;
      scores.each do |score|&lt;br /&gt;
        score.save&lt;br /&gt;
      end&lt;br /&gt;
      redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; map.id&lt;br /&gt;
    else&lt;br /&gt;
      flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
      redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; questionnaire.id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91423</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91423"/>
		<updated>2014-11-05T15:36:53Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt; consists of methods involved in creating, scoring &amp;amp; recording responses of the quizzes taken by reviewers or students of the other teams with the same topic. &lt;br /&gt;
&amp;lt;br&amp;gt;The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow principle of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database. By doing this, we followed the &amp;lt;b&amp;gt;Single Responsibility O-O Design Principle for methods&amp;lt;/b&amp;gt; mentioned by &amp;lt;b&amp;gt; Mr. Robert C. Martin &amp;lt;/b&amp;gt; which states that &amp;quot;Every software module should have only one reason to change&amp;quot;&amp;lt;ref&amp;gt;[http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles O-O Design Principles]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    questionnaire = Questionnaire.find(map.reviewed_object_id)&lt;br /&gt;
    scores = Array.new&lt;br /&gt;
    valid = true&lt;br /&gt;
    questions = Question.where(questionnaire_id: questionnaire.id)&lt;br /&gt;
    questions.each do |question|&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
      ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
      if ques_type.eql? 'MCC'&lt;br /&gt;
        if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        else&lt;br /&gt;
          params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
            correct_answers.each do |correct|&lt;br /&gt;
              if choice.eql? correct.txt&lt;br /&gt;
                score += 1&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
            end&lt;br /&gt;
            new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
            unless new_score.valid?&lt;br /&gt;
              valid = false&lt;br /&gt;
            end&lt;br /&gt;
            scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
            score = 1&lt;br /&gt;
          else&lt;br /&gt;
            score = 0&lt;br /&gt;
          end&lt;br /&gt;
          scores.each do |score_update|&lt;br /&gt;
            score_update.score = score&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = correct_answers.first&lt;br /&gt;
        if ques_type.eql? 'Essay'&lt;br /&gt;
          score = -1&lt;br /&gt;
        elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; response.id, :score =&amp;gt; score&lt;br /&gt;
        if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        end&lt;br /&gt;
        scores.push(new_score)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    if valid&lt;br /&gt;
      scores.each do |score|&lt;br /&gt;
        score.save&lt;br /&gt;
      end&lt;br /&gt;
      redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; map.id&lt;br /&gt;
    else&lt;br /&gt;
      flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
      redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; questionnaire.id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91422</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91422"/>
		<updated>2014-11-05T15:31:51Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt; consists of methods involved in creating, scoring &amp;amp; recording responses of the quizzes taken by reviewers or students of the other teams with the same topic. &lt;br /&gt;
&amp;lt;br&amp;gt;The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow principle of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database. By doing this, we followed the &amp;lt;b&amp;gt;Single Responsibility O-O Design Principle for methods&amp;lt;/b&amp;gt; which states that &amp;quot;Every software module should have only one reason to change&amp;quot;&amp;lt;ref&amp;gt;[http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles O-O Design Principles]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    questionnaire = Questionnaire.find(map.reviewed_object_id)&lt;br /&gt;
    scores = Array.new&lt;br /&gt;
    valid = true&lt;br /&gt;
    questions = Question.where(questionnaire_id: questionnaire.id)&lt;br /&gt;
    questions.each do |question|&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
      ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
      if ques_type.eql? 'MCC'&lt;br /&gt;
        if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        else&lt;br /&gt;
          params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
            correct_answers.each do |correct|&lt;br /&gt;
              if choice.eql? correct.txt&lt;br /&gt;
                score += 1&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
            end&lt;br /&gt;
            new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
            unless new_score.valid?&lt;br /&gt;
              valid = false&lt;br /&gt;
            end&lt;br /&gt;
            scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
            score = 1&lt;br /&gt;
          else&lt;br /&gt;
            score = 0&lt;br /&gt;
          end&lt;br /&gt;
          scores.each do |score_update|&lt;br /&gt;
            score_update.score = score&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = correct_answers.first&lt;br /&gt;
        if ques_type.eql? 'Essay'&lt;br /&gt;
          score = -1&lt;br /&gt;
        elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; response.id, :score =&amp;gt; score&lt;br /&gt;
        if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        end&lt;br /&gt;
        scores.push(new_score)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    if valid&lt;br /&gt;
      scores.each do |score|&lt;br /&gt;
        score.save&lt;br /&gt;
      end&lt;br /&gt;
      redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; map.id&lt;br /&gt;
    else&lt;br /&gt;
      flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
      redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; questionnaire.id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91421</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=91421"/>
		<updated>2014-11-05T15:28:57Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt; consists of methods involved in creating, scoring &amp;amp; recording responses of the quizzes taken by reviewers or students of the other teams with the same topic. &lt;br /&gt;
&amp;lt;br&amp;gt;The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow principle of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database. By doing this, we followed the &amp;lt;b&amp;gt;Single Responsibility O-O Design Principle for methods&amp;lt;/b&amp;gt; which states that &amp;quot;Every software module should have only one reason to change&amp;quot;&amp;lt;ref&amp;gt;http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    questionnaire = Questionnaire.find(map.reviewed_object_id)&lt;br /&gt;
    scores = Array.new&lt;br /&gt;
    valid = true&lt;br /&gt;
    questions = Question.where(questionnaire_id: questionnaire.id)&lt;br /&gt;
    questions.each do |question|&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
      ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
      if ques_type.eql? 'MCC'&lt;br /&gt;
        if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        else&lt;br /&gt;
          params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
            correct_answers.each do |correct|&lt;br /&gt;
              if choice.eql? correct.txt&lt;br /&gt;
                score += 1&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
            end&lt;br /&gt;
            new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
            unless new_score.valid?&lt;br /&gt;
              valid = false&lt;br /&gt;
            end&lt;br /&gt;
            scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
            score = 1&lt;br /&gt;
          else&lt;br /&gt;
            score = 0&lt;br /&gt;
          end&lt;br /&gt;
          scores.each do |score_update|&lt;br /&gt;
            score_update.score = score&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = correct_answers.first&lt;br /&gt;
        if ques_type.eql? 'Essay'&lt;br /&gt;
          score = -1&lt;br /&gt;
        elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; response.id, :score =&amp;gt; score&lt;br /&gt;
        if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        end&lt;br /&gt;
        scores.push(new_score)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    if valid&lt;br /&gt;
      scores.each do |score|&lt;br /&gt;
        score.save&lt;br /&gt;
      end&lt;br /&gt;
      redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; map.id&lt;br /&gt;
    else&lt;br /&gt;
      flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
      redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; questionnaire.id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90217</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90217"/>
		<updated>2014-10-29T01:15:15Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    questionnaire = Questionnaire.find(map.reviewed_object_id)&lt;br /&gt;
    scores = Array.new&lt;br /&gt;
    valid = true&lt;br /&gt;
    questions = Question.where(questionnaire_id: questionnaire.id)&lt;br /&gt;
    questions.each do |question|&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
      ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
      if ques_type.eql? 'MCC'&lt;br /&gt;
        if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        else&lt;br /&gt;
          params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
            correct_answers.each do |correct|&lt;br /&gt;
              if choice.eql? correct.txt&lt;br /&gt;
                score += 1&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
            end&lt;br /&gt;
            new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
            unless new_score.valid?&lt;br /&gt;
              valid = false&lt;br /&gt;
            end&lt;br /&gt;
            scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
            score = 1&lt;br /&gt;
          else&lt;br /&gt;
            score = 0&lt;br /&gt;
          end&lt;br /&gt;
          scores.each do |score_update|&lt;br /&gt;
            score_update.score = score&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = correct_answers.first&lt;br /&gt;
        if ques_type.eql? 'Essay'&lt;br /&gt;
          score = -1&lt;br /&gt;
        elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; response.id, :score =&amp;gt; score&lt;br /&gt;
        if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
          valid = false&lt;br /&gt;
        end&lt;br /&gt;
        scores.push(new_score)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    if valid&lt;br /&gt;
      scores.each do |score|&lt;br /&gt;
        score.save&lt;br /&gt;
      end&lt;br /&gt;
      redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; map.id&lt;br /&gt;
    else&lt;br /&gt;
      flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
      redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; questionnaire.id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90215</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90215"/>
		<updated>2014-10-29T01:11:10Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; function was performing two distinct operations : One operation was saving the response to the database and the other was to calculate the score for the questions. We created a new function &amp;lt;b&amp;gt;calculate_score&amp;lt;/b&amp;gt; that would calculate the score for the questions and &amp;lt;b&amp;gt;record_response&amp;lt;/b&amp;gt; now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90196</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90196"/>
		<updated>2014-10-29T00:48:27Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array stored almost the similar values. The &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array contained a copy of the value that the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array contained. Therefore we eliminated the &amp;lt;b&amp;gt;new_scores&amp;lt;/b&amp;gt; array and are performing all the operations only on the &amp;lt;b&amp;gt;scores&amp;lt;/b&amp;gt; array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90195</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90195"/>
		<updated>2014-10-29T00:41:44Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Changes made in method logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;b&amp;gt;score&amp;lt;/b&amp;gt; variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90194</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90194"/>
		<updated>2014-10-29T00:38:43Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Replace controller method with a model method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90192</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90192"/>
		<updated>2014-10-29T00:35:47Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Replace controller method with a model method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the &amp;lt;b&amp;gt;quiz_response_map&amp;lt;/b&amp;gt; model, since it queries that particular table in the database.&lt;br /&gt;
The code was extracted into the &amp;lt;b&amp;gt;get_mappings_for_reviewer&amp;lt;/b&amp;gt; method and placed in the file &amp;lt;b&amp;gt;quiz_response_map.rb&amp;lt;/b&amp;gt;. This method was then called from the &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt; controller.&lt;br /&gt;
&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models Fat Models and Skinny Controllers.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; quiz_response_map.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.get_mappings_for_reviewer(participant_id)&lt;br /&gt;
  return QuizResponseMap.where(reviewer_id: participant_id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90181</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90181"/>
		<updated>2014-10-29T00:11:48Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Use of Routing Helpers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code. Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90180</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90180"/>
		<updated>2014-10-29T00:11:02Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Use good conditional statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition. Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90179</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90179"/>
		<updated>2014-10-29T00:10:18Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Use good conditional statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90177</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90177"/>
		<updated>2014-10-29T00:08:09Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Used a Boolean variable when that is sufficient */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90176</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90176"/>
		<updated>2014-10-29T00:06:43Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Used a Boolean variable when that is sufficient */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90175</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90175"/>
		<updated>2014-10-29T00:05:57Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Used .nil? instead of &amp;quot;== nil&amp;quot; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90174</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90174"/>
		<updated>2014-10-29T00:05:30Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Used .nil? instead of &amp;quot;== nil&amp;quot; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
 &amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90173</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90173"/>
		<updated>2014-10-29T00:04:31Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Replace find_by_...  with a where command */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90172</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90172"/>
		<updated>2014-10-29T00:03:27Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Use key: ‘value’, not :key =&amp;gt; ‘value’ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90170</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90170"/>
		<updated>2014-10-29T00:01:37Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90169</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90169"/>
		<updated>2014-10-29T00:00:26Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Eliminated the &amp;quot;== true/false&amp;quot; check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90168</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90168"/>
		<updated>2014-10-28T23:59:53Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Used .eql? instead of &amp;quot;==&amp;quot; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90167</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90167"/>
		<updated>2014-10-28T23:58:09Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Code Cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : list&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90166</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90166"/>
		<updated>2014-10-28T23:55:58Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Pluralized the class name StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the &amp;lt;b&amp;gt;StudentQuiz&amp;lt;/b&amp;gt; controller class to &amp;lt;b&amp;gt;StudentQuizzes&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90164</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90164"/>
		<updated>2014-10-28T23:53:47Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Elimination of Boolean Zen */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90163</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90163"/>
		<updated>2014-10-28T23:53:02Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Elimination of Boolean Zen */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Method : graded?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90160</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90160"/>
		<updated>2014-10-28T23:50:13Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Instance Variable Reductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;take_quiz method&amp;lt;/b&amp;gt;, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90158</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90158"/>
		<updated>2014-10-28T23:42:18Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* RESTful style implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the &amp;lt;b&amp;gt;list&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;StudentQuizzes controller&amp;lt;/b&amp;gt; is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as &amp;lt;b&amp;gt;index&amp;lt;/b&amp;gt;. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : list&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; Method : index&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90157</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90157"/>
		<updated>2014-10-28T23:30:40Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90156</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90156"/>
		<updated>2014-10-28T23:28:46Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class. (StudentQuizzesController)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style. (e.g. :- Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method &amp;lt;b&amp;gt;graded?&amp;lt;/b&amp;gt; for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Perform Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Use good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Split a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fix logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90155</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90155"/>
		<updated>2014-10-28T23:24:43Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90154</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90154"/>
		<updated>2014-10-28T23:21:37Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Expertiza - Refactoring StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the &amp;lt;b&amp;gt;StudentQuiz controller&amp;lt;/b&amp;gt;. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90145</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90145"/>
		<updated>2014-10-28T21:25:18Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Modification to Existing Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifications made to the Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90144</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90144"/>
		<updated>2014-10-28T21:24:32Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Pluralized the class name StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
----&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90143</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90143"/>
		<updated>2014-10-28T21:24:18Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* RESTful style implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
----&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90142</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90142"/>
		<updated>2014-10-28T21:15:04Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Replace controller method with a model method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Replace controller method with a model method ===&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90141</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90141"/>
		<updated>2014-10-28T21:06:35Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Elimination of Boolean Zen */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;[https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf About Boolean Zen]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90140</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90140"/>
		<updated>2014-10-28T21:05:52Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Instance Variable Reductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages Disadvantages of Tight Coupling - Wikipedia]&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90138</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90138"/>
		<updated>2014-10-28T21:04:36Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Instance Variable Reductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them &amp;lt;ref&amp;gt;[http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability Decreasing Controller-View Coupling in Rails]&amp;lt;/ref&amp;gt;. Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
----&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90135</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90135"/>
		<updated>2014-10-28T21:02:46Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Pluralized the class name StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;[http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails Using the Singular or Plural controller and helper names in Rails]&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
----&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
----&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
----&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90129</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90129"/>
		<updated>2014-10-28T21:01:36Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our project requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
---&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90127</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90127"/>
		<updated>2014-10-28T21:01:08Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;[https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit# GoogleDoc for our assignment requirements]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
----&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90121</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90121"/>
		<updated>2014-10-28T20:59:27Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Expertiza - Refactoring StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki Page]&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
----&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90119</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90119"/>
		<updated>2014-10-28T20:58:47Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Expertiza - Refactoring StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
----&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
----&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
----&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
--&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90114</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90114"/>
		<updated>2014-10-28T20:56:05Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Expertiza - Refactoring StudentQuizController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)[https://github.com/expertiza/expertiza Expertiza on GitHub]&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
  @quizzes = Array.new&lt;br /&gt;
  reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
  @assignment = Assignment.find(assignment_id)&lt;br /&gt;
  teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
  Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
    unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
      Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
        if !@assignment.team_assignment?&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            @quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
               @quizzes.push(questionnaire)&lt;br /&gt;
             end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return @quizzes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
    quizzes = Array.new&lt;br /&gt;
    reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
    Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
      unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
        Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
            quizzes.push(questionnaire)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return quizzes&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90111</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90111"/>
		<updated>2014-10-28T20:47:21Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Code Cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;https://github.com/expertiza/expertiza&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
-    @quizzes = Array.new&lt;br /&gt;
+    quizzes = Array.new&lt;br /&gt;
     reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
-    @assignment = Assignment.find(assignment_id)&lt;br /&gt;
-    teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
     Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
       unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
         Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
-          if !@assignment.team_assignment?&lt;br /&gt;
-            unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
-              @quizzes.push(questionnaire)&lt;br /&gt;
-            end&lt;br /&gt;
-          else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
-            @quizzes.push(questionnaire)&lt;br /&gt;
+          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
+            quizzes.push(questionnaire)&lt;br /&gt;
           end&lt;br /&gt;
         end&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
+    return quizzes&lt;br /&gt;
   end&lt;br /&gt;
-  return @quizzes&lt;br /&gt;
-end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90110</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90110"/>
		<updated>2014-10-28T20:46:02Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Following Ruby Style Guidelines (Global Rules) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;https://github.com/expertiza/expertiza&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
-    @quizzes = Array.new&lt;br /&gt;
+    quizzes = Array.new&lt;br /&gt;
     reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
-    @assignment = Assignment.find(assignment_id)&lt;br /&gt;
-    teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
     Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
       unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
         Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
-          if !@assignment.team_assignment?&lt;br /&gt;
-            unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
-              @quizzes.push(questionnaire)&lt;br /&gt;
-            end&lt;br /&gt;
-          else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
-            @quizzes.push(questionnaire)&lt;br /&gt;
+          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
+            quizzes.push(questionnaire)&lt;br /&gt;
           end&lt;br /&gt;
         end&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
+    return quizzes&lt;br /&gt;
   end&lt;br /&gt;
-  return @quizzes&lt;br /&gt;
-end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Modified declarations of Arrays and Hashes&lt;br /&gt;
Before Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Removed unused methods like self.participants_in and commented out code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90108</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90108"/>
		<updated>2014-10-28T20:37:21Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Replace controller method with a model method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;https://github.com/expertiza/expertiza&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
-    @quizzes = Array.new&lt;br /&gt;
+    quizzes = Array.new&lt;br /&gt;
     reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
-    @assignment = Assignment.find(assignment_id)&lt;br /&gt;
-    teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
     Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
       unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
         Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
-          if !@assignment.team_assignment?&lt;br /&gt;
-            unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
-              @quizzes.push(questionnaire)&lt;br /&gt;
-            end&lt;br /&gt;
-          else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
-            @quizzes.push(questionnaire)&lt;br /&gt;
+          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
+            quizzes.push(questionnaire)&lt;br /&gt;
           end&lt;br /&gt;
         end&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
+    return quizzes&lt;br /&gt;
   end&lt;br /&gt;
-  return @quizzes&lt;br /&gt;
-end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Modified declarations of Arrays and Hashes&lt;br /&gt;
Before Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Removed unused methods like self.participants_in and commented out code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Method names should use underscores, not camelcase. ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good array checking====&lt;br /&gt;
&lt;br /&gt;
[].empty? # not [].length == 0 or [].length.zero?&lt;br /&gt;
[:foo].any? # not [:foo].length &amp;gt; 0&lt;br /&gt;
[:foo].one? # not [:foo].length == 1&lt;br /&gt;
[:foo].first # not [:foo][0]&lt;br /&gt;
[:foo].last # not [:foo][-1]&lt;br /&gt;
&lt;br /&gt;
====Use find_each for efficient loops over models====&lt;br /&gt;
&lt;br /&gt;
SignedUpUser.all.each do |user| # BAD&lt;br /&gt;
SignedUpUser.find_each do |user| # GOOD&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90107</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90107"/>
		<updated>2014-10-28T20:32:11Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Instance Variable Reductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;https://github.com/expertiza/expertiza&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables that are not used in the views.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
-    @quizzes = Array.new&lt;br /&gt;
+    quizzes = Array.new&lt;br /&gt;
     reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
-    @assignment = Assignment.find(assignment_id)&lt;br /&gt;
-    teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
     Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
       unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
         Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
-          if !@assignment.team_assignment?&lt;br /&gt;
-            unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
-              @quizzes.push(questionnaire)&lt;br /&gt;
-            end&lt;br /&gt;
-          else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
-            @quizzes.push(questionnaire)&lt;br /&gt;
+          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
+            quizzes.push(questionnaire)&lt;br /&gt;
           end&lt;br /&gt;
         end&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
+    return quizzes&lt;br /&gt;
   end&lt;br /&gt;
-  return @quizzes&lt;br /&gt;
-end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Modified declarations of Arrays and Hashes&lt;br /&gt;
Before Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Removed unused methods like self.participants_in and commented out code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Method names should use underscores, not camelcase. ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good array checking====&lt;br /&gt;
&lt;br /&gt;
[].empty? # not [].length == 0 or [].length.zero?&lt;br /&gt;
[:foo].any? # not [:foo].length &amp;gt; 0&lt;br /&gt;
[:foo].one? # not [:foo].length == 1&lt;br /&gt;
[:foo].first # not [:foo][0]&lt;br /&gt;
[:foo].last # not [:foo][-1]&lt;br /&gt;
&lt;br /&gt;
====Use find_each for efficient loops over models====&lt;br /&gt;
&lt;br /&gt;
SignedUpUser.all.each do |user| # BAD&lt;br /&gt;
SignedUpUser.find_each do |user| # GOOD&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quiz_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90106</id>
		<title>CSC/ECE 517 Fall 2014/oss E1460 aua</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2014/oss_E1460_aua&amp;diff=90106"/>
		<updated>2014-10-28T20:31:54Z</updated>

		<summary type="html">&lt;p&gt;Ajoshi5: /* Instance Variable Reductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Expertiza - Refactoring StudentQuizController==&lt;br /&gt;
Expertiza is a web application developed using Ruby on Rails that serves as a peer-review system. The application allows students to submit and peer-review learning objects (articles, code, web sites, etc)&amp;lt;ref&amp;gt;https://github.com/expertiza/expertiza&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://wikis.lib.ncsu.edu/index.php/Expertiza&amp;lt;/ref&amp;gt;. It is an open source project and it's codebase is maintained in GitHub. We are contributing to Expertiza as a part of our Object-Oriented Design and Development's Open-Source Software (OSS) Project. Our goal in this project is to refactor the Student Quiz Controller. In this Wiki Page, we would be explaining the changes that we have made for the same.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
Our Goal in this project is to refactor the StudentQuiz controller. This class records the quizzes that the student has attempted and its progress and also submits grades for the essays in the quizzes attempted by the student. The changes that are needed to be done are described as follows:&amp;lt;ref&amp;gt;https://docs.google.com/a/ncsu.edu/document/d/1FZCL9KWSdVNsX9BowuZ3gxbCOJoiWX-GVLctSZei3No/edit#&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pluralize the class (StudentQuizzesController).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename methods to conform to RESTful style (Rename the list method to index.)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Reduce the number of instance variables per controller action.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Review Method graded? for boolean zen.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Performing Code cleanup by removing unused code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Using good Ruby style guidelines in the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Splitting a method performing multiple tasks, into seperate methods&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Fixing logical errors in the code&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modification to Existing Code==&lt;br /&gt;
&lt;br /&gt;
===Pluralized the class name StudentQuizController===&lt;br /&gt;
As per the Rails convention the controller class names are suggested to be plural.&lt;br /&gt;
This helps in generating RESTful routing URI helpers. Also, naming the classes as plural seems intuitive.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails&amp;lt;/ref&amp;gt;. &lt;br /&gt;
We used the refactor functionality in RubyMine to rename the StudentQuiz controller class to StudentQuizzes.&lt;br /&gt;
&lt;br /&gt;
The following files were modified and/or renamed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/controllers/review_mapping_controller.rb&lt;br /&gt;
app/controllers/{student_quiz_controller.rb → student_quizzes_controller.rb}&lt;br /&gt;
app/helpers/student_quiz_helper.rb&lt;br /&gt;
app/helpers/student_quizzes_helper.rb&lt;br /&gt;
app/views/questionnaires/view.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_quiz_form.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_responses.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_dynamic_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/_set_self_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/finished_quiz.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/grade_essays.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/list.html.erb&lt;br /&gt;
app/views/{student_quiz → student_quizzes}/take_quiz.html.erb&lt;br /&gt;
app/views/student_task/view.html.erb&lt;br /&gt;
app/views/tree_display/actions/_assignments_actions.html.erb&lt;br /&gt;
test/functional/{student_quiz_controller_test.rb → student_quizzes_controller_test.rb}&lt;br /&gt;
test/test_helper.rb&lt;br /&gt;
test/unit/helpers/student_quiz_helper_test.rb&lt;br /&gt;
test/unit/helpers/student_quizzes_helper_test.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The detailed change-set for this refactor can be seen [https://github.com/ankit3005/expertiza/commit/20981333a14a3f468f76cd6d414b3088975b71bd here].&lt;br /&gt;
&lt;br /&gt;
===RESTful style implementation===&lt;br /&gt;
The purpose of the list method in the StudentQuizzes controller is to list all the quizzes that are available to a particular user for a particular assignment. RESTful guidelines state that a method that returns a list of all available objects, in this case the quizzes, should be named as index. Therefore, we renamed the list method to the index method in the controller and in all the files that had references to the list method of the student_quizzes_controller (For e.g. :- The view finished_quiz.html.erb of the Student Quiz controller, the review_mapping controller.rb etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring :&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Instance Variable Reductions===&lt;br /&gt;
In Rails, the data is shared between the controllers and views through the instance variables. The instance variabIes that are set during the execution a particular controller method are accessible to the view. Excessive usage of this standard method of data sharing between the controller and the view results in increased coupling between them. &amp;lt;ref&amp;gt;http://blog.remarkablelabs.com/2013/01/how-to-decrease-coupling-in-your-controllers-views-with-decent_exposure-for-better-maintainability&amp;lt;/ref&amp;gt; Increased coupling results in several problems, including less maintainability of code, difficulty in code reuse etc. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Disadvantages&amp;lt;/ref&amp;gt; Thus, we need to reduce coupling as much as possible and in this case it can be achieved by reducing as many instance variables as possible in the controller. Therefore, we have refactored our code in order to eliminate unnecessary instance variables and to convert all the instance variables to local variables if they are not used in the views.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&lt;br /&gt;
In the take_quiz method, the following instance variables were removed: &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
* Changed the &amp;lt;b&amp;gt;quizzes&amp;lt;/b&amp;gt; instance variable to a local variable.&lt;br /&gt;
* Eliminated the instance variable &amp;lt;b&amp;gt;assignment&amp;lt;/b&amp;gt; which was not being used anywhere.&lt;br /&gt;
* Eliminated the local variable &amp;lt;b&amp;gt;teams&amp;lt;/b&amp;gt; which was not being used any where&lt;br /&gt;
The unified diff of the changes made is shown below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def self.take_quiz assignment_id , reviewer_id&lt;br /&gt;
-    @quizzes = Array.new&lt;br /&gt;
+    quizzes = Array.new&lt;br /&gt;
     reviewer = Participant.where(user_id: reviewer_id, parent_id: assignment_id).first&lt;br /&gt;
-    @assignment = Assignment.find(assignment_id)&lt;br /&gt;
-    teams = TeamsUser.where(user_id: reviewer_id)&lt;br /&gt;
     Team.where(parent_id: assignment_id).each do |quiz_creator|&lt;br /&gt;
       unless TeamsUser.find_by_team_id(quiz_creator.id).user_id == reviewer_id&lt;br /&gt;
         Questionnaire.where(instructor_id: quiz_creator.id).each do |questionnaire|&lt;br /&gt;
-          if !@assignment.team_assignment?&lt;br /&gt;
-            unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
-              @quizzes.push(questionnaire)&lt;br /&gt;
-            end&lt;br /&gt;
-          else unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer_id).first&lt;br /&gt;
-            @quizzes.push(questionnaire)&lt;br /&gt;
+          unless QuizResponseMap.where(reviewed_object_id: questionnaire.id, reviewer_id:  reviewer.id).first&lt;br /&gt;
+            quizzes.push(questionnaire)&lt;br /&gt;
           end&lt;br /&gt;
         end&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
+    return quizzes&lt;br /&gt;
   end&lt;br /&gt;
-  return @quizzes&lt;br /&gt;
-end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Elimination of Boolean Zen===&lt;br /&gt;
In cases where a method only returns a boolean value by evaluating an expression (using the if..else construct), the if..else statement is redundant and can be eliminated. We have a method &amp;lt;b&amp;gt; graded? &amp;lt;/b&amp;gt; that does this thing. Thus, we eliminated the if..else construct in the graded? method as it was redundant.&amp;lt;ref&amp;gt;https://www.cs.utexas.edu/~scottm/cs312/handouts/slides/topic16_boolean_logic.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  if Score.where(question_id: question.id, response_id:  response.id).first&lt;br /&gt;
    return true&lt;br /&gt;
  else&lt;br /&gt;
    return false&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def graded?(response, question)&lt;br /&gt;
  return (Score.where(question_id: question.id, response_id:  response.id).first)&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Cleanup===&lt;br /&gt;
At some places, we found certain statements and variable assignments that were not being used later on in the method or code. We have eliminated such unused statements, so that methods contain only code that is being used later on. This would improve the readability and the maintainability of the code. For example in the code below we eliminated the &amp;lt;b&amp;gt;@quiz_phase&amp;lt;/b&amp;gt; and the &amp;lt;b&amp;gt;@num_quizzes_total&amp;lt;/b&amp;gt; variables because they were not being used anywhere in the code. Also, since the &amp;lt;b&amp;gt;local&amp;lt;/b&amp;gt; variables &amp;lt;b&amp;gt;quiz_due_date, deadline_type_id, participant&amp;lt;/b&amp;gt; were not being used anywhere, we could eliminate the entire &amp;lt;b&amp;gt;if&amp;lt;/b&amp;gt; block from the code below. We then converted the instance variable &amp;lt;b&amp;gt;participant&amp;lt;/b&amp;gt; to a local variable because it was not being used in the corresponding view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @assignment = Assignment.find(@participant.parent_id)&lt;br /&gt;
&lt;br /&gt;
    # Find the current phase that the assignment is in.&lt;br /&gt;
    @quiz_phase = @assignment.get_current_stage(AssignmentParticipant.find(params[:id]).topic_id)&lt;br /&gt;
&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.where(reviewer_id: @participant.id)&lt;br /&gt;
&lt;br /&gt;
    # Calculate the number of quizzes that the user has completed so far.&lt;br /&gt;
    @num_quizzes_total = @quiz_mappings.size&lt;br /&gt;
&lt;br /&gt;
    @num_quizzes_completed = 0&lt;br /&gt;
    @quiz_mappings.each do |map|&lt;br /&gt;
      @num_quizzes_completed += 1 if map.response&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if @assignment.staggered_deadline?&lt;br /&gt;
      @quiz_mappings.each { |quiz_mapping|&lt;br /&gt;
        if @assignment.team_assignment?&lt;br /&gt;
          participant = AssignmentTeam.get_first_member(quiz_mapping.reviewee_id)&lt;br /&gt;
        else&lt;br /&gt;
          participant = quiz_mapping.reviewee&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if !participant.nil? and !participant.topic_id.nil?&lt;br /&gt;
          quiz_due_date = TopicDeadline.where(topic_id: participant.topic_id, deadline_type_id: 1).first&lt;br /&gt;
        end&lt;br /&gt;
      }&lt;br /&gt;
      deadline_type_id = DeadlineType.find_by_name('quiz').id&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
    @assignment = Assignment.find(participant.parent_id)&lt;br /&gt;
    @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Modified declarations of Arrays and Hashes&lt;br /&gt;
Before Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After Refactoring :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Removed unused methods like self.participants_in and commented out code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Following Ruby Style Guidelines (Global Rules)===&lt;br /&gt;
At many places in the code we found that the Ruby Style Guidelines were not followed. We have refactored the code so that it uses the good code style guidelines. The following are some of the refactorings the we have done:-&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== Used .eql? instead of &amp;quot;==&amp;quot; ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score == -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if score.score.eql? -1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Eliminated the &amp;quot;== true/false&amp;quot; check ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method : finished_quiz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded == true &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; if essay_not_graded &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Use `&amp;amp;&amp;amp;` and `||` rather than `and` and `or` to keep boolean precedence ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt; elsif  correct_answer &amp;amp;&amp;amp; params[&amp;quot;#{question.id}&amp;quot;]== correct_answer.txt &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Method names should use underscores, not camelcase. ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Use key: ‘value’, not :key =&amp;gt; ‘value’ ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Replace find_by_...  with a where command ====&lt;br /&gt;
Rails 4 conventions dictate the use of 'where()' over the use of 'find_by_...' methods while querying ActiveRecords. The code has been refactored to replace the usages of find_by.. with where().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Before Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt; if (QuestionType.find_by_question_id question.id).q_type == 'MCC' &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; After Refactoring &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
if ques_type.eql? 'MCC' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good array checking====&lt;br /&gt;
&lt;br /&gt;
[].empty? # not [].length == 0 or [].length.zero?&lt;br /&gt;
[:foo].any? # not [:foo].length &amp;gt; 0&lt;br /&gt;
[:foo].one? # not [:foo].length == 1&lt;br /&gt;
[:foo].first # not [:foo][0]&lt;br /&gt;
[:foo].last # not [:foo][-1]&lt;br /&gt;
&lt;br /&gt;
====Use find_each for efficient loops over models====&lt;br /&gt;
&lt;br /&gt;
SignedUpUser.all.each do |user| # BAD&lt;br /&gt;
SignedUpUser.find_each do |user| # GOOD&lt;br /&gt;
	&lt;br /&gt;
====Used .nil? instead of &amp;quot;== nil&amp;quot; ====&lt;br /&gt;
The inbuilt method .nil? returns a boolean. &lt;br /&gt;
&amp;lt;br&amp;gt; we have used this to replace an == nil check that was there in the code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Used a Boolean variable when that is sufficient====&lt;br /&gt;
It is a good practice to use a boolean variable if we only need a boolean for our purpose.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = 1&lt;br /&gt;
 if valid == 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
valid = false&lt;br /&gt;
 if valid&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use good conditional statements====&lt;br /&gt;
Using unless is a good practice. But it is not a good practice to use unless and ! within the condition.&lt;br /&gt;
&amp;lt;br&amp;gt; Instead we could use an if condition itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: record_response&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quizzes_controller.rb&lt;br /&gt;
&amp;lt;br&amp;gt; Method: calculate_score&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if new_score.comments.empty? || new_score.comments.nil?&lt;br /&gt;
   valid = false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use of Routing Helpers====&lt;br /&gt;
Routing helpers are a simpler alternative to the otherwise complex hard coded URLs which reduce the readability of the code.Routing helpers allow us to declare possible common routes for a given controller. Routing helpers have been implemented since they maintain consistency even if changes are made to the routing paths. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;:&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb does not contain a student_quizzes resource&lt;br /&gt;
&amp;lt;br&amp;gt; review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'index', :id =&amp;gt; params[:participant_id]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; config.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resources :student_quizzes, :only =&amp;gt; [:index]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
review_mapping_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
redirect_to student_quizzes_path(:id =&amp;gt; params[:participant_id])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Replace controller method with a model method ====&lt;br /&gt;
Rails 4 conventions dictate the use of a fat model and skinny controller.&lt;br /&gt;
It is better to put place the search function in the model rather than placing it in the controller.&lt;br /&gt;
The search code belonged to the quiz_response_map model, since it queries that particular table in the DB.&lt;br /&gt;
The code was extracted into the subsequent method displayed below and called from the invitation controller.&amp;lt;ref name = &amp;quot;stackoverflow&amp;quot;&amp;gt;[http://stackoverflow.com/questions/14044681/fat-models-and-skinny-controllers-sounds-like-creating-god-models StackOverflow]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; student_quiz_controller.rb&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.where(reviewer_id: participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 @quiz_mappings = QuizResponseMap.get_mappings_for_reviewer(participant.id)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Changes made in method logic===&lt;br /&gt;
We have made certain changes in the logic of the methods calculate_score and record_response (previously the code of both these methods was only in record_response) primarily to improve the existing logic and eliminate redundant code. These changes are described as follows:&lt;br /&gt;
&lt;br /&gt;
* The score variable was already being set to 0 on the loop entry, therefore it was redundant to reset score to zero again. Thus, we eliminated this line of code inside the if statement.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|		&lt;br /&gt;
  score = 0		&lt;br /&gt;
  if (QuestionType.find_by_question_id question.id).q_type == 'MCC'		&lt;br /&gt;
    score = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if ques_type.eql? 'MCC'&lt;br /&gt;
    # Eliminated score = 0 over here&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; stored multiple values as the where condition to which it was assigned was returning multiple values. Therefore it seemed more intuitive to rename &amp;lt;b&amp;gt;correct_answer&amp;lt;/b&amp;gt; to &amp;lt;b&amp;gt;correct_answers&amp;lt;/b&amp;gt; so that it is apparent that it contains multiple values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The below piece of code found out the question type twice in the same loop. Therefore we extracted it and assigned it to a local variable so that the query is executed only once.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# First Query&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
# Repetition of the query in the same loop&lt;br /&gt;
if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Querying only once and assigning it to a local variable ques_type&lt;br /&gt;
ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
# Usage 1 of ques_type&lt;br /&gt;
if ques_type.eql? 'MCC'&lt;br /&gt;
# Usage 2 of ques_type&lt;br /&gt;
if ques_type.eql? 'Essay'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The new_scores and scores array stored almost the similar values. The scores contained a copy of the value that the new_scores array contained. Therefore we eliminated the new_scores array and are performing all the operations only on the scores array.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
 valid = 1&lt;br /&gt;
end&lt;br /&gt;
new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
# Part 2		  &lt;br /&gt;
new_scores.each do |score_update|&lt;br /&gt;
 score_update.score = score&lt;br /&gt;
 scores.push(score_update)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1&lt;br /&gt;
new_score = Score.new comments: choice, question_id: question.id, response_id: response.id&lt;br /&gt;
&lt;br /&gt;
unless new_score.valid?&lt;br /&gt;
  valid = false&lt;br /&gt;
end&lt;br /&gt;
scores.push(new_score)&lt;br /&gt;
	&lt;br /&gt;
# Part 2		&lt;br /&gt;
scores.each do |score_update|&lt;br /&gt;
  score_update.score = score&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The logic to compute the final score for a multiple-choice, multiple-correct type of question seemed to be incorrect and therefore we fixed it.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
  score = 0&lt;br /&gt;
  if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
   valid = 1&lt;br /&gt;
  else&lt;br /&gt;
    correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
    params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
    correct_answer.each do |correct|&lt;br /&gt;
    if choice == correct.txt&lt;br /&gt;
      score += 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic which seems to award full points even if you marked some extra options apart from marking all correct answers&lt;br /&gt;
  unless score == correct_answer.count&lt;br /&gt;
   score = 0&lt;br /&gt;
  else&lt;br /&gt;
   score = 1&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Part 1 of the Scoring Logic&lt;br /&gt;
questions.each do |question|&lt;br /&gt;
 score = 0&lt;br /&gt;
 correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)&lt;br /&gt;
 ques_type = (QuestionType.where( question_id: question.id)).q_type&lt;br /&gt;
  if ques_type.eql? 'MCC'&lt;br /&gt;
     if params[&amp;quot;#{question.id}&amp;quot;].nil?&lt;br /&gt;
       valid = false&lt;br /&gt;
     else&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
          correct_answers.each do |correct|&lt;br /&gt;
          if choice.eql? correct.txt&lt;br /&gt;
             score += 1&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
# Part 2 of the scoring logic - We have also compared the number of options the user selected to the total number of correct answers&lt;br /&gt;
if score.eql? correct_answers.count &amp;amp;&amp;amp; score == params[&amp;quot;#{question.id}&amp;quot;].count&lt;br /&gt;
  score = 1&lt;br /&gt;
else&lt;br /&gt;
  score = 0&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The record_response function was performing two distinct operations : One operation was saving the response to the Database and the other was to calculate the score for the questions. We created a new function calculate_score that would calculate the score for the questions and record_response now only performs the task of saving responses to the database.&lt;br /&gt;
&amp;lt;b&amp;gt;Before Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def record_response&lt;br /&gt;
  @map = ResponseMap.find(params[:map_id])&lt;br /&gt;
  @response = Response.new()&lt;br /&gt;
  @response.map_id = params[:map_id]&lt;br /&gt;
  @response.created_at = DateTime.current&lt;br /&gt;
  @response.updated_at = DateTime.current&lt;br /&gt;
  @response.save&lt;br /&gt;
&lt;br /&gt;
  @questionnaire = Questionnaire.find(@map.reviewed_object_id)&lt;br /&gt;
  scores = Array.new&lt;br /&gt;
  new_scores = Array.new&lt;br /&gt;
  valid = 0&lt;br /&gt;
  questions = Question.where(questionnaire_id: @questionnaire.id)&lt;br /&gt;
  questions.each do |question|&lt;br /&gt;
    score = 0&lt;br /&gt;
    if (QuestionType.find_by_question_id question.id).q_type == 'MCC'&lt;br /&gt;
      score = 0&lt;br /&gt;
      if params[&amp;quot;#{question.id}&amp;quot;] == nil&lt;br /&gt;
        valid = 1&lt;br /&gt;
      else&lt;br /&gt;
        correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect: 1)&lt;br /&gt;
        params[&amp;quot;#{question.id}&amp;quot;].each do |choice|&lt;br /&gt;
&lt;br /&gt;
          correct_answer.each do |correct|&lt;br /&gt;
            if choice == correct.txt&lt;br /&gt;
              score += 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
          new_score = Score.new :comments =&amp;gt; choice, :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id&lt;br /&gt;
&lt;br /&gt;
          unless new_score.valid?&lt;br /&gt;
            valid = 1&lt;br /&gt;
          end&lt;br /&gt;
          new_scores.push(new_score)&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
        unless score == correct_answer.count&lt;br /&gt;
          score = 0&lt;br /&gt;
        else&lt;br /&gt;
          score = 1&lt;br /&gt;
        end&lt;br /&gt;
        new_scores.each do |score_update|&lt;br /&gt;
          score_update.score = score&lt;br /&gt;
          scores.push(score_update)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      score = 0&lt;br /&gt;
      correct_answer = QuizQuestionChoice.where(question_id: question.id, iscorrect:  1).first&lt;br /&gt;
      if (QuestionType.find_by_question_id question.id).q_type == 'Essay'&lt;br /&gt;
        score = -1&lt;br /&gt;
      elsif  correct_answer and params[&amp;quot;#{question.id}&amp;quot;] == correct_answer.txt&lt;br /&gt;
        score = 1&lt;br /&gt;
      end&lt;br /&gt;
      new_score = Score.new :comments =&amp;gt; params[&amp;quot;#{question.id}&amp;quot;], :question_id =&amp;gt; question.id, :response_id =&amp;gt; @response.id, :score =&amp;gt; score&lt;br /&gt;
      unless new_score.comments != &amp;quot;&amp;quot; &amp;amp;&amp;amp; new_score.comments&lt;br /&gt;
        valid = 1&lt;br /&gt;
      end&lt;br /&gt;
      scores.push(new_score)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  if valid == 0&lt;br /&gt;
    scores.each do |score|&lt;br /&gt;
      score.save&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to :controller =&amp;gt; 'student_quizzes', :action =&amp;gt; 'finished_quiz', :map_id =&amp;gt; @map.id&lt;br /&gt;
  else&lt;br /&gt;
    flash[:error] = &amp;quot;Please answer every question.&amp;quot;&lt;br /&gt;
    redirect_to :action =&amp;gt; :take_quiz, :assignment_id =&amp;gt; params[:assignment_id], :questionnaire_id =&amp;gt; @questionnaire.id&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;After Refactoring&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# New record_response method&lt;br /&gt;
  def record_response&lt;br /&gt;
    map = ResponseMap.find(params[:map_id])&lt;br /&gt;
    response = Response.new&lt;br /&gt;
    response.map_id = params[:map_id]&lt;br /&gt;
    response.created_at = DateTime.current&lt;br /&gt;
    response.updated_at = DateTime.current&lt;br /&gt;
    response.save&lt;br /&gt;
&lt;br /&gt;
    calculate_score map,response&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# New calculate_score&lt;br /&gt;
def calculate_score map, response&lt;br /&gt;
    # The entire logic below the response.save line from the earlier record_response method goes here.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ajoshi5</name></author>
	</entry>
</feed>