Passed
Push — master ( 3df1d0...111a66 )
by Serhii
02:56 queued 01:25
created

timeago.getRules   C

Complexity

Conditions 10

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 10

Importance

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

How to fix   Complexity   

Complexity

Complex classes like timeago.getRules 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 timeago
2
3
import "github.com/SerhiiCho/timeago/models"
4
5
func getRules(number, lastDigit int) map[string]models.Rule {
6 1
	return map[string]models.Rule{
7
		"en": {
8
			Single: number == 1,
9
			Plural: number > 1 || number == 0,
10
		},
11
		"ru": {
12
			Single: lastDigit == 1 || number == 0,
13
			Plural: lastDigit >= 2 && lastDigit < 5,
14
			Special: []bool{
15
				number >= 5 && number <= 20,
16
				lastDigit == 0,
17
				lastDigit >= 5 && lastDigit <= 9,
18
			},
19
		},
20
		"uk": {
21
			Single: lastDigit == 1 || number == 0,
22
			Plural: lastDigit >= 2 && lastDigit < 5,
23
			Special: []bool{
24
				number >= 5 && number <= 20,
25
				lastDigit == 0,
26
				lastDigit >= 5 && lastDigit <= 9,
27
			},
28
		},
29
	}
30
}
31