func deletePosts
has a cyclomatic complexity of 17 with "high" risk157 server.ServeError(writer, errStr, wantsJSON, nil)
158}
159
160func deletePosts(checkedPosts []int, writer http.ResponseWriter, request *http.Request) {161 // Delete post(s) or thread(s)
162 infoEv, errEv := gcutil.LogRequest(request)
163 defer gcutil.LogDiscard(infoEv, errEv)
func CallManageFunction
has a cyclomatic complexity of 25 with "high" risk 37
38// CallManageFunction is called when a user accesses /manage to use manage tools
39// or log in to a staff account
40func CallManageFunction(writer http.ResponseWriter, request *http.Request) { 41 var err error
42 wantsJSON := serverutil.IsRequestingJSON(request)
43 accessEv := gcutil.LogAccess(request)
func getBoardDataFromForm
has a cyclomatic complexity of 24 with "high" risk207
208// getBoardDataFromForm parses the relevant form fields into the board and returns any errors for invalid string to int
209// or missing required fields. It should only be used for editing and creating boards
210func getBoardDataFromForm(board *gcsql.Board, request *http.Request) error {211 requestType, _, _ := boardsRequestType(request)
212
213 staff, err := getCurrentStaff(request)
func MakePost
has a cyclomatic complexity of 55 with "critical" risk 97}
98
99// MakePost is called when a user accesses /post. Parse form data, then insert and build
100func MakePost(writer http.ResponseWriter, request *http.Request) {101 request.ParseMultipartForm(maxFormBytes)
102 wantsJSON := serverutil.IsRequestingJSON(request)
103
func AttachUploadFromRequest
has a cyclomatic complexity of 23 with "high" risk 74// AttachUploadFromRequest reads an incoming HTTP request and processes any incoming files.
75// It returns the upload (if there was one) and whether or not any errors were served (meaning
76// that it should stop processing the post
77func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter, post *gcsql.Post, postBoard *gcsql.Board) (*gcsql.Upload, error) { 78 infoEv, errEv := gcutil.LogRequest(request)
79 defer func() {
80 gcutil.LogDiscard(infoEv, errEv)
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. |