func handleBlockOperationEvents
has a cyclomatic complexity of 26 with "very-high" risk154 }
155}
156
157func handleBlockOperationEvents(w http.ResponseWriter, flusher http.Flusher, requestedTopics map[string]bool, event *feed.Event) error {158 switch event.Type {
159 case operation.AggregatedAttReceived:
160 if _, ok := requestedTopics[AttestationTopic]; !ok {
func CommitteeAssignments
has a cyclomatic complexity of 16 with "high" risk157// 2. Compute all committees.
158// 3. Determine the attesting slot for each committee.
159// 4. Construct a map of validator indices pointing to the respective committees.
160func CommitteeAssignments(161 ctx context.Context,
162 state state.BeaconState,
163 epoch primitives.Epoch,
func ProcessRegistryUpdates
has a cyclomatic complexity of 16 with "high" risk 91// for index in activation_queue[:get_validator_churn_limit(state)]:
92// validator = state.validators[index]
93// validator.activation_epoch = compute_activation_exit_epoch(get_current_epoch(state))
94func ProcessRegistryUpdates(ctx context.Context, state state.BeaconState) (state.BeaconState, error) { 95 currentEpoch := time.CurrentEpoch(state)
96 vals := state.Validators()
97 var err error
func VerifyAttestationNoVerifySignature
has a cyclomatic complexity of 22 with "high" risk 43
44// VerifyAttestationNoVerifySignature verifies the attestation without verifying the attestation signature. This is
45// used before processing attestation with the beacon state.
46func VerifyAttestationNoVerifySignature( 47 ctx context.Context,
48 beaconState state.ReadOnlyBeaconState,
49 att interfaces.Attestation,
func TranslateParticipation
has a cyclomatic complexity of 23 with "high" risk145// for index in get_attesting_indices(state, data, attestation.aggregation_bits):
146// for flag_index in participation_flag_indices:
147// epoch_participation[index] = add_flag(epoch_participation[index], flag_index)
148func TranslateParticipation(ctx context.Context, state state.BeaconState, atts []*ethpb.PendingAttestation) (state.BeaconState, error) {149 epochParticipation, err := state.PreviousEpochParticipation()
150 if err != nil {
151 return nil, err
func EpochParticipation
has a cyclomatic complexity of 16 with "high" risk180// if flag_index in participation_flag_indices and not has_flag(epoch_participation[index], flag_index):
181// epoch_participation[index] = add_flag(epoch_participation[index], flag_index)
182// proposer_reward_numerator += get_base_reward(state, index) * weight
183func EpochParticipation(beaconState state.BeaconState, indices []uint64, epochParticipation []byte, participatedFlags map[uint8]bool, totalBalance uint64) (uint64, []byte, error) {184 cfg := params.BeaconConfig()
185 sourceFlagIndex := cfg.TimelySourceFlagIndex
186 targetFlagIndex := cfg.TimelyTargetFlagIndex
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:
- Break the function into smaller, more manageable functions.
- Refactor complex logic into separate functions or classes.
- Avoid multiple return paths and deeply nested control expressions.
Bad practice
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
Recommended
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
Issue configuration
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. |