Passed
Push — v2.2.0 ( 414314 )
by Serhii
01:40
created

timeago.getRules   F

Complexity

Conditions 14

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 19
dl 0
loc 23
rs 3.6
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
	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
		"de": {
30
			Single: number == 1,
31
			Plural: number > 1 || number == 0,
32
		},
33
	}
34
}
35