getLicences
has a cyclomatic complexity of 27 with "very-high" risk 22 searchString?: string
23}
24
25export default function getLicences( 26 filters: GetLicencesFilters,
27 options: {
28 limit: number
15 <i class="fas fa-3x fa-circle-notch fa-spin" aria-hidden="true"></i><br />
16 <em>Loading records...</em>
17 </p>`;
18 cityssm.postJSON(`${glm.urlPrefix}/licences/doSearch`, formElement, (rawResponseJSON) => { 19 var _a, _b, _c, _d, _e, _f, _g, _h;
20 const licenceResults = rawResponseJSON;
21 const licenceList = licenceResults.licences;
getLicences
has a cyclomatic complexity of 27 with "very-high" risk 3import { licencesDB as databasePath } from '../data/databasePaths.js';
4import getLicenceFields from './getLicenceFields.js';
5import getLicenceTransactions from './getLicenceTransactions.js';
6export default function getLicences(filters, options) { 7 const database = sqlite(databasePath, {
8 readonly: true
9 });
A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.
To reduce the cyclomatic complexity of a function, you can:
// When `cyclomatic_complexity_threshold` is set to `low`, by default it is `high`
function getCapitalCity(countryName) {
if (countryName === 'India') {
return 'New Delhi'
} else if (countryName === 'China') {
return 'Beijing'
} else if (countryName === 'France') {
return 'Paris'
} else if (countryName === 'Germany') {
return 'Berlin'
} else if (countryName === 'Italy') {
return 'Rome'
}
}
function getCapitalCity(countryName) {
const capitalCities = {
India: 'New Delhi',
China: 'Beijing',
France: 'Paris',
Germany: 'Berlin',
Italy: 'Rome'
}
return capitalCities[countryName]
}
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
meta field in the
.deepsource.toml
config file.
Configuring this is optional. If you don't provide a value, the Analyzer will
raise issues for functions with complexity higher than the default threshold,
which is high
(only raises for issues >25) for the JavaScript Analyzer.
Here's the mapping of the risk category to the cyclomatic complexity score to help you configure this better:
Risk category | Cyclomatic complexity range | Recommended action |
---|---|---|
low | 1-5 | No action needed. |
medium | 6-15 | Review and monitor. |
high | 16-25 | Review and refactor. Recommended to add comments if the function is absolutely needed to be kept as it is. |
very-high | 26-50 | Refactor to reduce the complexity. |
critical | >50 | Must refactor this. This can make the code untestable and very difficult to understand. |