func getTextAndDate
has a cyclomatic complexity of 18 with "high" risk199 y int
200}
201
202func (widget *Widget) getTextAndDate(text string) (string, *time.Time) {203 now := time.Now()
204 textLower := strings.ToLower(text)
205 // check for "in X days/weeks/months/years" pattern
func encodeGetParams
has a cyclomatic complexity of 17 with "high" risk49 return nil
50}
51
52func encodeGetParams(params map[string]interface{}) string {53 s := url.Values{}
54 for k, v := range params {
55 switch val := v.(type) {
func nbascore
has a cyclomatic complexity of 23 with "high" risk 42 widget.Redraw(widget.nbascore)
43}
44
45func (widget *Widget) nbascore() (string, string, bool) { 46 title := widget.CommonSettings().Title
47 cur := time.Now().AddDate(0, 0, offset) // Go back/forward offset days
48 curString := cur.Format("20060102") // Need 20060102 format to feed to api
func displayStatus
has a cyclomatic complexity of 18 with "high" risk24 return title, str, false
25}
26
27func (widget *Widget) displayStatus() string {28 status, err := widget.client.MonStatus()
29
30 if err != nil || len(status.Lines) == 0 {
func MakeWidget
has a cyclomatic complexity of 89 with "critical" risk 91)
92
93// MakeWidget creates and returns instances of widgets
94func MakeWidget( 95 tviewApp *tview.Application,
96 pages *tview.Pages,
97 moduleName string,
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:
package main
import "log"
func fizzbuzzfuzz(x int) { // cc = 1
if x == 0 || x < 0 { // cc = 3 (if, ||)
return
}
for i := 1; i <= x; i++ { // cc = 4 (for)
switch i % 15 * 2 {
case 0: // cc = 5 (case)
countDiv3 += 1
countDiv5 += 1
log.Println("fizzbuzz")
break
case 3:
case 6:
case 9:
case 12: // cc = 9 (case)
countDiv3 += 1
log.Println("fizz")
break
case 5:
case 10: // cc = 11 (case)
countDiv5 += 1
log.Println("buzz")
break
default:
log.Printf("%d\n", x)
}
}
} // CC == 11; raises issues
package main
import "log"
func fizzbuzz(x int) { // cc = 1
for i := 1; i <= x; i++ { // cc = 2 (for)
y := i%3 == 0
z := i%5 == 0
if y == z { // 3
if y == false { // 4
log.Printf("%d\n", i)
} else {
log.Println("fizzbuzz")
}
} else {
if y { // 5
log.Println("fizz")
} else {
log.Println("buzz")
}
}
}
} // CC == 5
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
(docs) 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 medium
(only raise issues for >15) for the Go 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. |