CSC/ECE 517 Spring 2019 - E1919 CodeClimate Issues

From Expertiza_Wiki
Jump to navigation Jump to search

This wiki page is for the description of changes made according to the specification of E1919, OSS project - Fix Code Climate issues in models with names beginning with 'H' thru 'Sc'.

About Expertiza

Expertiza is a platform where assignments and related quizzes are managed. On this portal instructor upload assignments/questionnaires, create list of topics for students and edit the existing assignments. Students can form teams, work on projects and submit their assignments as URL or files. It also helps in improving the work quality by allowing students to provide anonymous reviews. It is an Open source platform which is based on Ruby with Rails.

Problem Statement

The existing code of "Expertiza" has many code smells which are not in compliance with the best practices of Ruby Rails. These smells were detected by CodeClimate.

Code climate is a command line interface for the Code Climate analysis platform. It allows you to run Code Climate engines on your local machine inside of Docker containers. It provides automated code review for test coverage and maintainability.

Objective

We were assigned the task to resolve code climate issues in models with names beginning with ‘H’ thru ‘Sc’.

As per instructions, we were to fix all the issues detected by code climate except for below. Fix all code smells except

  • Assignment Branch Condition size for [method name] is too high
  • Perceived complexity for [method name] is too high.
  • Cyclomatic complexity for [method name] is too high.
  • Method [method name] has a Cognitive Complexity of XX (exceeds 5 allowed). Consider refactoring.
  • File [file name] has XXX lines of code (exceeds 250 allowed). Consider refactoring.
  • Class [class name] has XX methods (exceeds 20 allowed). Consider refactoring.
  • Method [method name] has XX lines of code (exceeds 25 allowed). Consider refactoring.

Test Plan

1. Set up CodeClimate locally on our systems by using the following guide.

    https://docs.google.com/document/d/1ZmYdjJ4CeXyBc8-7EFslmCzHw8FF6hsSg3EjL0Vlr8w/edit

2. Use "analyze" command.

We have used "analyze" command of Code Climate to find out the code smells.

Analyze command lets you pass file paths as arguments to the codeclimate and also lets you specify engine options with -e or --engine.

codeclimate analyze -e rubocop <file_Name> 

3. We have also used Rubocop documentation for resolving issues.

RuboCop is a Ruby static code analyzer (a.k.a. linter) and code formatter.

It enforces the guidelines outlined in the community Ruby Style Guide for its best practices.

4. Issue related to method names starting with get_ are ignored because it requires refactoring across multiple files in the project, which would in turn break existing functionalities.

5. some issues are ignored as they were breaking the rspec tests. e.g.

  • mass assignment : It is used to create an instance using multiple parameters
  • In some test files instance were created with multiple parameter along with the primary id. But in mass assignment primary id is dangerous if allowed in mass assignment.


Examples

  • mass assignment
Mass assignment refers to the act of constructing your object with a parameters hash. 

We assign multiple values to attributes via a single assignment operator.

It is implemented using attr_accessible method. It takes a list of attributes that will be accessible. All other attributes will be protected.



  • Active Record Associations : Adding "inverse_of" option.

Associated objects do not point to the same in-memory objects by default.

When you have two models in a has_many, has_one or belongs_to association, the :inverse_of option in Rails tells ActiveRecord that they're two sides of the same association.

Knowing the other side of the same association Rails can optimize object loading. It lets you reference the same object in memory, instead of loading another copy of the same record.


  • Add dependent property

In the file roles.rb we have has_many :users

We need to specify "dependent" option here.

With associations of Active Record , we can make transitions smooth of such operations(relationships) by telling Rails that there is a connection between the two models. It would make more sense here if we add "dependent :destroy" option for users. If a particular role is deleted, all the users under that role must also disappear.

dependent :destroy
dependent :nullify



  • useless assignment of a variable

In the file role.rb, we have

The variable e is not being used anywhere. Such values are assigned to "_" or rather do not assign them to any variable, so that it doesn't get assigned to a variable and uses space.

rescue StandardError 

  • Using snake case for method names

In file roles.rb, we have

A standard good practice is to follow snake cases for naming methods.

  • Use find_by instead of where.first

In file scored_question.rb, we have

Rails prefers "find_by" over "where" as "find_by" returns a single record as result, whereas "where" returns an array of record, to which we need to append "first" or "each" to get single record out of it.

answer = Answer.find_by(question_id: self.id, response_id: response_id) 
  • Nested ternary operators

Ternary operators must not be nested. Rather we should prefer if-else.


And, there were many more code smells which needed a fix.

Results

  • 162 issues resolved
  • 42 Files changed
  • 215 additions
  • 163 Deletions
  • Travis CI build passed
  • No merge conflicts
  • coveralls :
       *  Coverage increased (+10.1%) to 50.97%.
  • codeclimate bot
       * Code Climate has analyzed commit 050fa7d and detected 0 issues on this pull request.
       * The test coverage on the diff in this pull request is 42.3% (50% is the threshold).
       * This pull request will bring the total coverage in the repository to 29.0% (-21.9% change).
  • travis-ci
       * Your changes look good to me! tada
       * The Travis CI build passed

Files Modified for this requirement

  • app/models/http_request.rb
  • app/models/import_error.rb
  • app/models/institution.rb
  • app/models/instructor.rb
  • app/models/invitation.rb
  • app/models/join_team_request.rb
  • app/models/language.rb
  • app/models/late_policy.rb
  • app/models/logger_message.rb
  • app/models/markup_style.rb
  • app/models/menu_item.rb
  • app/models/menu.rb
  • app/models/metareview_questionnaire.rb
  • app/models/metareview_response_map.rb
  • app/models/missing_object_id_error.rb
  • app/models/multiple_choice_checkbox.rb
  • app/models/multiple_choice_radio.rb
  • app/models/node.rb
  • app/models/notification.rb
  • app/models/on_the_fly_calc.rb
  • app/models/participant.rb
  • app/models/password_reset.rb
  • app/models/path_error.rb
  • app/models/permission.rb
  • app/models/plagiarism_checker_assignment_submission.rb
  • app/models/plagiarism_checker_comparison.rb
  • app/models/question_advice.rb  
  • app/models/questionnaire_header.rb
  • app/models/questionnaire_node.rb
  • app/models/questionnaire.rb
  • app/models/questionnaire_type_node.rb
  • app/models/question.rb
  • app/models/quiz_assignment.rb
  • app/models/quiz_question_choice.rb
  • app/models/quiz_questionnaire.rb
  • app/models/quiz_question.rb
  • app/models/quiz_response_map.rb
  • app/models/quiz_response.rb
  • app/models/requested_user.rb
  • app/models/response_map.rb
  • app/models/response.rb  
  • app/models/review_assignment.rb
  • app/models/review_comment_paste_bin.rb
  • app/models/review_grade.rb
  • app/models/review_questionnaire.rb
  • app/models/review_response_map.rb
  • app/models/role.rb
  • app/models/rscore.rb
  • app/models/rubric.rb  
  • app/models/scale.rb
  • app/models/scored_question.rb
  • app/models/score_view.rb

In additions to above files, we have edited the following files as well as they were dependency on those.

  • ta.rb

  • response.html.erb

  • spec/models/scale_spec.rb

Total Modifications

42 Files changed

215 additions

163 Deletions

Important Links