E1563. Refactoring the Questionnaires Controller


Introduction

edit

Questionnaire Controller is responsible for creating, editing and reviewing quizes. The Questionnaire controller is also used for peer-review, teammate-review and creating and managing quizes. The basic functionality of adding, removing, editing questions and or options is handled by this controller. This wikipedia page explains the Open Source Project of refactoring the questionnaire controller on expertiza.

The need of refactoring

  1. Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp.[1] This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
  2. Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.[2]

Project Overview

edit

The scope of our project is to refactor the questionnaires controller. The questionnaires controller at this moment is very big and hence, difficult to understand. The aim of the project was to distribute the functionality in different controllers, so that the questionnaires controller will be slim and easy to understand.

Classes involved

edit
  • questionnaire.rb
  • quiz_questionnaire.rb
  • questions_controller.rb

Changes made

edit
  • Functionality moved to quiz_questionnaire.rb.
  • edit_advice method was not being used, so it was removed.
  • save_advice moved to the advice_controller.
  • copy, update_quiz, valid_quiz methods were long and have been broken up. clone_questionnaire_details was also broken up and renamed.
  • Added comments to select_questionnaire_type
  • Debug output (print statements) have been removed.
  • Changed code to follow the global rules.
  • save_new_questions, delete_questions, save_questions have been moved to a separate class.
  • A lot of code duplication has been removed.
  • Test cases are written using rspec for the functions in questionnaire_controller.rb.
S.No. Place changes implemented Change Made Reason
1 Complete controller Replaced 'and' with '&&' and 'or' with 'II' in all places Refactored as per global rule 1 to keep Boolean precedence
2 Complete controller Replaced if (var == true) with if var Refactored as per global rule 4 as default check for 'if' is for true
3 Complete controller Replaced :key => ‘value’ with key: ‘value’ for 88 instance in controller Refactored as per global rules
4 assign_instructor_id Removed duplicate code for assigning instructor id from copy and clone_questionnaire_details methods Removed duplication suggested by code climate.
5 check_create_new_node Removed duplicate code for checking creation of new node from copy_questionnaire,save and clone_questionnaire_details methods Removed duplication suggested by code climate.
6 update_quiz Refactored this method and extracted choose_question_type out of this method Refactoring was needed for this huge method
7 valid_quiz Refactored this method and extracted valid_quiz_option out of this method to check validity of options Refactoring was needed as method was performing 2 actions
8 save_choices Refactored this method and created 3 new methods namely save_truefalse_choice,save_single_choice and save_multiple_choice Refactoring was needed for this huge method

Project Resources

edit

GitHub Project Repository

VCL IP Address

Expertiza Wiki


Code Snippets

edit

Future Consideration

edit
  • Controller was very big than what the name describes. Appropriate functionality needs to be moved to different controllers to make them slim.
  • The Quiz methods are to be treated as any different kind of questionnaire, and the functionality be implemented in the model class of quiz_questionnaire.
  • Turn the questionnaire into a “form object.” The ..._questions methods: save_new_questions, delete_questions, save_questions should be in a separate class.
  • There is a valid_quiz in this controller, but it only check if the quiz is valid when new quiz is created, but not when the existing quiz is updated. The reason is that, the format for params is not the same. Please fix this issue and make the valid _quiz work both for quiz creation and quiz_update.
  • Write tests for quiz creation, for both scenario which the input quiz questions are valid and not valid.
  • Test (and fix if they are broken) the export and import functionality. Write test for importing and exporting questionnaires (Issue 577).

Object Oriented Design Principles Followed

edit

The following object oriented design principles were followed during refactoring:

  • Single Responsibility Principle: The single responsibility principle states that every context (class, function, variable, etc.) should have a single responsibility. It was maintained that each method should be involved with a single responsibility and the code that was not related to that particular functionality was moved to other method. This was also taken care of in terms of classes.
  • DRY Principle: Don't Repeat Yourself! The repetitive and redundant code was removed from the associated classes and methods.

Running Project & Testing Functionality

edit
  • Test Cases have been implemented for the functionality in quiz_controller.rb
  • This testing has been done using RSpec, a behavior-driven development framework.

Conclusion

edit

References

edit

Expertiza
Code Refactoring Wiki
Global Rules for Refactoring
Why Refactor
Single Responsibility Principle

  1. ^ Martin, Robert (2009). Clean Code. Prentice Hall.
  2. ^ Cite error: The named reference kerievsky was invoked but never defined (see the help page).