func marshalState
has a cyclomatic complexity of 24 with "high" risk602}
603
604// marshal versioned state from struct type down to bytes.
605func marshalState(ctx context.Context, st state.ReadOnlyBeaconState) ([]byte, error) {606 switch st.ToProtoUnsafe().(type) {
607 case *ethpb.BeaconState:
608 rState, ok := st.ToProtoUnsafe().(*ethpb.BeaconState)
func unmarshalState
has a cyclomatic complexity of 26 with "very-high" risk508}
509
510// unmarshal state from marshaled proto state bytes to versioned state struct type.
511func (s *Store) unmarshalState(_ context.Context, enc []byte, validatorEntries []*ethpb.Validator) (state.BeaconState, error) {512 var err error
513 enc, err = snappy.Decode(nil, enc)
514 if err != nil {
func saveStatesEfficientInternal
has a cyclomatic complexity of 16 with "high" risk213 return validatorKeys, validatorsEntries, nil
214}
215
216func (s *Store) saveStatesEfficientInternal(ctx context.Context, tx *bolt.Tx, blockRoots [][32]byte, states []state.ReadOnlyBeaconState, validatorKeys [][]byte, validatorsEntries map[string]*ethpb.Validator) error {217 bucket := tx.Bucket(stateBucket)
218 valIdxBkt := tx.Bucket(blockRootValidatorHashesBucket)
219 for i, rt := range blockRoots {
func unmarshalBlock
has a cyclomatic complexity of 22 with "high" risk769}
770
771// unmarshal block from marshaled proto beacon block bytes to versioned beacon block struct type.
772func unmarshalBlock(_ context.Context, enc []byte) (interfaces.ReadOnlySignedBeaconBlock, error) {773 var err error
774 enc, err = snappy.Decode(nil, enc)
775 if err != nil {
func deepValueEqualExportedOnly
has a cyclomatic complexity of 35 with "very-high" risk121 }
122}
123
124func deepValueEqualExportedOnly(v1, v2 reflect.Value, visited map[visit]bool, depth int) bool {125 if !v1.IsValid() || !v2.IsValid() {
126 return v1.IsValid() == v2.IsValid()
127 }
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. |