| Conditions | 12 |
| Total Lines | 43 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 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 | } |
||
| 57 |