data.*rangedTextGenerator.generateRangedText   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 29
nop 2
dl 0
loc 47
ccs 27
cts 27
cp 1
crap 10
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like data.*rangedTextGenerator.generateRangedText often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package data
2
3
import "syreclabs.com/go/faker"
4
5
type rangedTextGenerator struct {
6
	random randomGenerator
7
}
8
9
func (generator *rangedTextGenerator) generateRangedText(minLength int, maxLength int) string {
10 1
	if maxLength < 5 {
11 1
		return faker.RandomString(maxLength)
12
	}
13
14 1
	length := minLength
15 1
	if maxLength-minLength > 0 {
16 1
		length = generator.random.Intn(maxLength-minLength) + minLength
17
	}
18
19 1
	text := ""
20
21 1
	maxWordsCount := length / 10
22 1
	if maxWordsCount < 2 {
23 1
		maxWordsCount = 2
24 1
	} else if maxWordsCount > 9 {
25 1
		maxWordsCount = 9
26
	}
27
28 1
	for {
29 1
		wordsCount := generator.random.Intn(maxWordsCount) + 1
30 1
		sentence := faker.Lorem().Sentence(wordsCount)
31
32 1
		var extendedText string
33 1
		if len(text) == 0 {
34 1
			extendedText = sentence
35
		} else {
36 1
			extendedText = text + " " + sentence
37
		}
38
39 1
		if len(extendedText) >= length {
40 1
			if len(extendedText) >= maxLength {
41 1
				break
42
			}
43
44 1
			text = extendedText
45 1
			break
46
		}
47
48 1
		text = extendedText
49
	}
50
51 1
	if len(text) < minLength {
52 1
		text = faker.RandomString(minLength)
53
	}
54
55 1
	return text
56
}
57