Virtual Code Review
unleash/src/lib/services/project-service.ts
Detailed Recommendations
Complex Method
Where?
One method has high complexity.
- ProjectService.changeProject(cc = 9)
Why does this problem occur?
A Complex Method has a high cyclomatic complexity. The recommended threshold for the TypeScript language is a cyclomatic complexity lower than 9.
Severity: Brain Method - Complex Method - Long Method.
How to fix it?
There are many reasons for Complex Method. Sometimes, another design approach is beneficial such as a) modeling state using an explicit state machine rather than conditionals, or b) using table lookup rather than long chains of logic. In other scenarios, the function can be split using EXTRACT FUNCTION. Just make sure you extract natural and cohesive functions. Complex Methods can also be addressed by identifying complex conditional expressions and then using the DECOMPOSE CONDITIONAL refactoring.
Act: refactoring recommendation
To get a general understanding of what this code health issue looks like - and how it might be addressed - we have prepared some diffs for illustrative purposes.
@@ -1,15 +1,20 @@ |
function postItem(item) { |
if (!item.id) { |
if (item.x != null && item.y != null) { |
post(item); |
} else { |
throw Error("Item must have x and y"); |
} |
// extract a separate function for creating new item |
postNew(item); |
} else { |
if (item.x < 10 && item.y > 25) { |
put(item); |
} else { |
throw Error("Item must have an x and y value between 10 and 25"); |
} |
// and one for updating existing items |
updateItem(item); |
} |
} |
function postNew(item) { |
validateNew(item); |
post(item); |
} |
function updateItem(item) { |
validateUpdate(item); |
put(item); |
} |
Duplicated Function Blocks
Where?
The module contains 10 functions with similar structure.
Why does this problem occur?
Duplicated code often leads to code that's harder to change since the same logical change has to be done in multiple functions. More duplication gives lower code health.
How to fix it?
A certain degree of duplicated code might be acceptable. The problems start when it is the same behavior that is duplicated across the functions in the module, ie. a violation of the Don't Repeat Yourself (DRY) principle. DRY violations lead to code that is changed together in predictable patterns, which is both expensive and risky. DRY violations can be identified using CodeScene's X-Ray analysis to detect clusters of change coupled functions with high code similarity. Read More
Once you have identified the similarities across functions, look to extract and encapsulate the concept that varies into its own function(s). These shared abstractions can then be re-used, which minimizes the amount of duplication and simplifies change.
Large Method
Where?
You have 1 functions that exceed the threshold.
- ProjectService.getStatusUpdates(LoC = 77 lines)
Why does this problem occur?
Overly long functions make the code harder to read. The recommended maximum function length for the TypeScript language is 70 lines of code. Severity: Brain Method - Complex Method - Long Method.
How to fix it?
We recommend to be careful here -- just splitting long functions don't necessarily make the code easier to read. Instead, look for natural chunks inside the functions that expresses a specific task or concern. Often, such concerns are indicated by a Code Comment followed by an if-statement. Use the EXTRACT FUNCTION refactoring to encapsulate that concern.
Excess Number of Function Arguments
Where?
This file has 10 functions that exceed the maximum number of arguments.
- ProjectService.constructor(Arguments = 8)
- ProjectService.removeUser(Arguments = 5)
- ProjectService.addGroup(Arguments = 5)
- ProjectService.removeGroup(Arguments = 5)
- ProjectService.addRoleAccess(Arguments = 5)
- ProjectService.addAccess(Arguments = 6)
- ProjectService.setRolesForUser(Arguments = 5)
- ProjectService.setRolesForGroup(Arguments = 5)
- ProjectService.changeRole(Arguments = 5)
- ProjectService.changeGroupRole(Arguments = 5)
Why does this problem occur?
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the TypeScript language is 4 function arguments.
How to fix it?
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
Primitive Obsession
Where?
All functions in the file
Why does this problem occur?
Code that uses a high degree of built-in, primitives such as integers, strings, floats, lacks a domain language that encapsulates the validation and semantics of function arguments. Primitive Obsession has several consequences: 1) In a statically typed language, the compiler will detect less erroneous assignments. 2) Security impact since the possible value range of a variable/argument isn't retricted.
In this module, 79 % of all functions have primitive types as arguments.
How to fix it?
Primitive Obsession indicates a missing domain language. Introduce data types that encapsulate the details and constraints of your domain. For example, instead of int userId
, consider User clicked
.
Act: refactoring recommendation
To get a general understanding of what this code health issue looks like - and how it might be addressed - we have prepared some diffs for illustrative purposes.
@@ -1,8 +1,8 @@ |
// Problem: It's hard to know what this function does, and what values are valid as parameters. |
function getPopularRepositories(String baseURL, String query, Integer pages, Integer pageSize, String sortorder): Json { |
let pages == null ? 10 : pages |
let pageSize == null ? 10 : pageSize |
return httpClient.get(`${baseURL}?q=${query}&pages=${pages}&pageSize=${pageSize}&sortorder=${sortorder}`) |
// Refactoring: extract the pagination & API logic into a class, and it will |
// attract validation and other logic related to the specific query. It's now |
// easier to use and to maintain the getPopularRepositories function! |
function getPopularRepositories(query: PaginatedRepoQuery): Json { |
return httpClient.get(query.getURL()) |
.map(json => json.repositories) |
.filter(repository => repositry.stargazersCount > 1000) |
} |
String Heavy Function Arguments
Where?
All functions in the file
Why does this problem occur?
String is a generic type that fail to capture the constraints of the domain object it represents. In this module, 40 % of all function arguments are string types.
How to fix it?
Heavy string usage indicates a missing domain language. Introduce data types that encapsulate the semantics. For example, a user_name is better represented as a constrained User type rather than a pure string, which could be anything.
Plan a goal for this Hotspot
Goal
No active goal set.
Complexity Trends
Costs per Type of Work
Change Coupling
Waiting for change coupling data.