Passed
Push — master ( 21cf41...510e0f )
by Serhii
02:06 queued 30s
created

timeago.getRules   D

Complexity

Conditions 13

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 16
dl 0
loc 19
ccs 1
cts 1
cp 1
crap 13
rs 4.2
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
type Rule struct {
4
	Single  bool
5
	Plural  bool
6
	Special bool
7
}
8
9
func getRules(number, lastDigit int) map[string]Rule {
10 1
	return map[string]Rule{
11
		"en": {
12
			Single: number == 1,
13
			Plural: number > 1 || number == 0,
14
		},
15
		"ru": {
16
			Special: (number >= 5 && number <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
17
			Single:  lastDigit == 1,
18
			Plural:  lastDigit >= 2 && lastDigit < 5,
19
		},
20
		"uk": {
21
			Special: (number >= 5 && number <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
22
			Single:  lastDigit == 1,
23
			Plural:  lastDigit >= 2 && lastDigit < 5,
24
		},
25
		"nl": {
26
			Single: number == 1,
27
			Plural: number > 1 || number == 0,
28
		},
29
	}
30
}
31