Passed
Push — v3.0.0 ( 4fda89...e47893 )
by Serhii
01:18
created

timeago.getRules   D

Complexity

Conditions 12

Size

Total Lines 43
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 35
nop 1
dl 0
loc 43
rs 4.8
c 0
b 0
f 0

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
	Zero  bool
5
	One   bool
6
	Two   bool
7
	Few   bool
8
	Many  bool
9
	Other bool
10
}
11
12
func identifyGrammarRules(num int) map[string]rule {
13
	lastDigit := num % 10
14
15
	return map[string]rule{
16
		"en": {
17
			Zero:  num == 0,
18
			One:   num == 1,
19
			Few:   num > 1,
20
			Two:   num == 2,
21
			Many:  num > 1,
22
			Other: num > 1,
23
		},
24
		"ru": {
25
			// Zero: num == 0,
26
			One: lastDigit == 1,
27
			// Two:  num == 2,
28
			Few:  lastDigit == 2 || lastDigit == 3 || lastDigit == 4,
29
			Many: (num >= 5 && num <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
30
		},
31
		"uk": {
32
			Zero:  num == 0,
33
			One:   lastDigit == 1,
34
			Two:   num == 2,
35
			Few:   num == 2,
36
			Many:  lastDigit >= 2 && lastDigit < 5,
37
			Other: (num >= 5 && num <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
38
		},
39
		"nl": {
40
			Zero:  num == 0,
41
			One:   num == 1,
42
			Few:   num > 1,
43
			Two:   num == 2,
44
			Many:  num > 1,
45
			Other: num > 1,
46
		},
47
		"de": {
48
			Zero:  num == 0,
49
			One:   num == 1,
50
			Few:   num > 1,
51
			Two:   num == 2,
52
			Many:  num > 1,
53
			Other: num > 1,
54
		},
55
	}
56
}
57