Conditions | 22 |
Total Lines | 70 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like timeago.applyCustomTranslations 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 |
||
48 | func applyCustomTranslations(langSet *LangSet) *LangSet { |
||
49 | if len(conf.Translations) == 0 { |
||
50 | return langSet |
||
51 | } |
||
52 | |||
53 | for _, trans := range conf.Translations { |
||
54 | if trans.Lang != langSet.Lang { |
||
55 | continue |
||
56 | } |
||
57 | |||
58 | if trans.Format != "" { |
||
59 | langSet.Format = trans.Format |
||
60 | } |
||
61 | |||
62 | if trans.Ago != "" { |
||
63 | langSet.Ago = trans.Ago |
||
64 | } |
||
65 | |||
66 | if trans.Online != "" { |
||
67 | langSet.Online = trans.Online |
||
68 | } |
||
69 | |||
70 | if trans.JustNow != "" { |
||
71 | langSet.JustNow = trans.JustNow |
||
72 | } |
||
73 | |||
74 | if trans.Second != nil { |
||
75 | for k, v := range trans.Second { |
||
76 | langSet.Second[k] = v |
||
77 | } |
||
78 | } |
||
79 | |||
80 | if trans.Minute != nil { |
||
81 | for k, v := range trans.Minute { |
||
82 | langSet.Minute[k] = v |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if trans.Hour != nil { |
||
87 | for k, v := range trans.Hour { |
||
88 | langSet.Hour[k] = v |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if trans.Day != nil { |
||
93 | for k, v := range trans.Day { |
||
94 | langSet.Day[k] = v |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if trans.Week != nil { |
||
99 | for k, v := range trans.Week { |
||
100 | langSet.Week[k] = v |
||
101 | } |
||
102 | } |
||
103 | |||
104 | if trans.Month != nil { |
||
105 | for k, v := range trans.Month { |
||
106 | langSet.Month[k] = v |
||
107 | } |
||
108 | } |
||
109 | |||
110 | if trans.Year != nil { |
||
111 | for k, v := range trans.Year { |
||
112 | langSet.Year[k] = v |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return langSet |
||
118 | } |
||
119 |