CSC/ECE 517 Spring 2020 E2009 Refactor assignment: Difference between revisions
No edit summary |
|||
Line 26: | Line 26: | ||
== Refactoring smaller Issues == | == Refactoring smaller Issues == | ||
1. Removing unused variables | |||
2. Changing variable names | |||
3. Avoiding multi-line ternary operators | |||
Bad Coding Practice | |||
a = cond ? | |||
b : c | |||
a = cond ? b : | |||
c | |||
a = cond ? | |||
b : | |||
c | |||
Good Coding Practice | |||
a = cond ? b : c | |||
a = | |||
if cond | |||
b | |||
else | |||
c | |||
end | |||
- Using Guard Clause instead o wrapping the code inside a conditional expression | - Using Guard Clause instead o wrapping the code inside a conditional expression |
Revision as of 21:56, 23 March 2020
Introduction
Expertiza allows instructors to add assignments and students to upload their submissions. The assignment.rb model file consists of some basic CRUD operations along with some methods which help calculate scores and export details etc. The goal of the project is to refactor assignment.rb file to follow good coding practices.
Refactoring assignment.rb
Some of the coding issues with the assignment.rb file are 1) Methods performing more than one tasks, resulting in long methods 2) Methods with many conditional branches with multiple conditions and loops resulting in increased cyclomatic and cognitive complexity 3) Dead code 4) No proper naming conventions in some places. 5) Large number of methods in one file.
Approach
The approach we took to refactor this file, is to go through the issues generated by code climate and fix the smaller issues first. This gave us an idea about the code is doing and gave us a head start to fix bigger issues. 69 issues were found on code climate and through this project, 30-35 issues have been resolved. Few of the issues that were resolved was detected by rubocop.
Refactoring longer methods
We refactored the longer methods mostly using extract method. The methods refactored using Extract method are scores method and review_questionnaire_id method. The scores method was the biggest method in assignment.rb file. The scores methods computes and returns the scores of participants and teams as a hash. The participant scores and directly got by calling a method in participant. For the teams, there can two possibilities. Scores method has nested loop structures along with many lines code performing a distinct action inside the nested loop. Hence first we extracted a method performing from the inner most nested level
Refactoring smaller Issues
1. Removing unused variables
2. Changing variable names
3. Avoiding multi-line ternary operators
Bad Coding Practice a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b : c
Good Coding Practice a = cond ? b : c
a =
if cond
b
else
c
end
- Using Guard Clause instead o wrapping the code inside a conditional expression A guard clause is simply a check that immediately exits the function, either with a return statement or an exception. Bad Coding Practice if something
raise 'exception'
else
ok
end
Good Coding Practice raise 'exception' if something ok
- Reducing Cognitive Complexity Cyclomatic complexity is a static analysis measure of how difficult is code to test. Cognitive complexity tells us, how difficult code is to understand by a reader.
- Reducing Cyclomatic Complexity Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.
- Reducing Perceived Complexity This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers `when` nodes as something that doesn't add as much complexity as an `if` or a `&&`. Except if it's one of those special `case`/`when` constructs where there's no expression after `case`. Then the cop treats it as an `if`/`elsif`/`elsif`… and lets all the `when` nodes count. In contrast to the CyclomaticComplexity cop, this cop considers `else` nodes as adding complexity.
- ABC Size This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions.