User:Svenka15

From Expertiza_Wiki
Jump to navigation Jump to search

CSC/ECE 517 Fall 2016/E1672 Remove duplicated code in feature tests


Introduction

This project mainly focuses on removing duplicated codes in feature tests and using Code Climate Chrome Extension and Travis CI to evaluate the modified code.

Background

Expertiza is an educational web application created and maintained by the joint efforts of the students and the faculty at NCSU. It’s an open source project developed on Ruby on Rails platform and the code is available on Github. It's a web portal used by students for assignment submission and team selection among other activities as listed below

  • It allows students to submit their work- either a wiki page or a deployed link.
  • Review other’s work and grade them.
  • Make changes according to user reviews
  • Allows the instructor to create new assignments and customize new or existing assignments.
  • Allows the instructor to create a list of topics the students can sign up for.
  • Students can bid for topics of their interest by assigning priorities to the topics
  • Students can form teams in Expertiza to work on various projects and assignments.

Project Description

The following is an Expertiza based OSS project which deals with the files in spec/features folder, to be specific: assignment_creation_spec.rb, instructor_interface_spec.rb, questionnaire_spec.rb, quiz_spec.rb, delayed_mailer_spec.rb and scheduled_task_spec.rb. It focuses on removing the duplicate codes, following DRY concepts, change DateTime class object invocation, etc. The goal of this project is to attempt to make this part of the application easier to read, maintain and improve the quality of the code. The existing redundant codes, use of long methods, incorrect method invocation and other errors are pointed out by the Code climate extension which are modified in this project. The code climate extension also provides a rating for all files as well as an overall score for the entire application, to help us make proper modifications.

Problem Statement

The following tasks were accomplished in this project:

Install the Code Climate Chrome Extension, and when you go to Expertiza repo, you will see the extension will highlight the duplication code. Improved the clarity of code by improving the variable and parameter names. Remove the duplicated code by extracting a new method, moving code to before(:each) block, etc. Long character strings were taken and given appropriate names. Improve the Code Climate rating of classes whose original rating is F. Do not delete existing test cases and keep them pass TravisCI after refactoring. Files modified in current project

Six files in spec/features that are to be modified are :

Code Climate

Code Climate is a open and extensible platform for static analysis and works on the principle: healthy code ships faster. Code climate platform leads to healthier code by clear and actionable static analysis regardless of the technology used.

The static analysis engines and all the static analysis code used are published under Open Source licenses. This paves way for the users to dig in and understand how the code is being evaluated.

The rating and GPA provided by Code Climate extension is an estimate of the quality of the code. The ratings vary from A to F and the GPA varies from 1 to 5. Code with a higher GPA is expected to have higher quality.

Getting started with Code Climate

To get started with Code Climate the following 4 steps are to be followed

User perspective

  • Download Code climate and allow the extension to be used with your browser
  • Sign into your Github account and add the repository to be checked by clicking the extension from your browser.

  • Manually add the repository from the dashboard.

List of changes

Similar code found in 1 other location (mass = 50) Duplicated code can lead to software that is hard to understand and difficult to change. Refactorings -Extract Method,Extract Class,Form Template Method,Introduce Null Object,Pull Up Method,Pull Up Field,Substitute Algorithm Use find_by instead of dynamic find_by_name. Do not use DateTime.now without zone.

The files in the project include duplicated code which violates DRY concept and is to be removed. The various changes that can be made to the files are explained.

1. Similar code found in 1 other location

When there is duplicated code within the file or between files of the same project, the code can be created as a function with variables that are assigned based on the different cases. This function can be called wherever the functionality is required to be fulfilled and also by passing the required parameters. This makes the code more readable and in fact, much easier to understand. By writing similar lines of code again and again, we only increase the number of lines and decrease the efficiency of the code.For example, the following error was pointed out by Code Climate Extension in assignment_creation_spec.rb

Similar code was found

code similarity

New functions were added to remove duplicates

code similarity


2. Use find_by instead of dynamic find_by_name.

In the different spec files, find_by_name function is used to identify variables according to their name. find_by can be used to match the argument with the variable directly, instead of making sure if the function parameters are matching with the given file.


For example in file assignment_creation_spec.rb, find_by_name was detected:


This was corrected to find_by



The grade of the code improved when find_by_name was changed to find_by.

3. Refactoring - Extract Method,Extract Class,Form Template Method,Introduce Null Object,Pull Up Method,Pull Up Field,Substitute Algorithm

Refactoring is the process of improving a source code without altering the external behaviour. It improves Extensibility and Maintainability. We can implement refactoring using one of the methods mentioned above.


A common code that is present across files can be added to rails_helper.rb and called across files when needed. One example of this is the cod in integration_test_instructor_interface function which was typed out without being encapsulated as a function in both instructor_interface_spec.rb and questionnaire_spec.rb.

A common definition was added to rails_helper.rb for the integration_test_instructor_interface function as shown below


4. Useless Assignments of Variables

Same variables may be assigned values through the program which may not be used later. These assignments are said to be useless as the new value never gets used anywhere. These lines can be removed from the program as they do not implement anything and are of no use.

And another example:

Useless assignment

These assignments were removed


5.Do not use DateTime.now without Time.zone.now

To use the current time in the program, we need use Time.zone.now instead of DateTime.now. The latter uses the system's date and time as it is part of the Ruby standard library. It is better to use Time.zone.now because it returns the time that is corrected according to our corresponding Time zone as it has been extended by ActiveSupport. Example is as given :


6. Indentation

Programs need to be indented properly. 2 White spaces are used to write statements within any block. Accordingly, the end of the block statements also follow the corresponding white spaces. Indentation improves code readability and makes it easy for the user/programmer to understand what happens within the program.

Results and grades

The grades of the 6 files changed after the duplicates were removed Before modification : After modification: