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