| Conditions | 2 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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:
| 1 | package gotil_test |
||
| 129 | func TestSortDescBy(t *testing.T) { |
||
| 130 | input := []user{ |
||
| 131 | { |
||
| 132 | name: "Micheal", |
||
| 133 | age: 27, |
||
| 134 | location: location{ |
||
| 135 | city: "New York", |
||
| 136 | }, |
||
| 137 | }, |
||
| 138 | { |
||
| 139 | name: "Joe", |
||
| 140 | age: 30, |
||
| 141 | location: location{ |
||
| 142 | city: "Detroit", |
||
| 143 | }, |
||
| 144 | }, |
||
| 145 | { |
||
| 146 | name: "Olivia", |
||
| 147 | age: 42, |
||
| 148 | location: location{ |
||
| 149 | city: "New York", |
||
| 150 | }, |
||
| 151 | }, |
||
| 152 | { |
||
| 153 | name: "Kevin", |
||
| 154 | age: 10, |
||
| 155 | location: location{ |
||
| 156 | city: "Boston", |
||
| 157 | }, |
||
| 158 | }, |
||
| 159 | } |
||
| 160 | expected := []user{ |
||
| 161 | { |
||
| 162 | name: "Olivia", |
||
| 163 | age: 42, |
||
| 164 | location: location{ |
||
| 165 | city: "New York", |
||
| 166 | }, |
||
| 167 | }, |
||
| 168 | { |
||
| 169 | name: "Joe", |
||
| 170 | age: 30, |
||
| 171 | location: location{ |
||
| 172 | city: "Detroit", |
||
| 173 | }, |
||
| 174 | }, |
||
| 175 | { |
||
| 176 | name: "Micheal", |
||
| 177 | age: 27, |
||
| 178 | location: location{ |
||
| 179 | city: "New York", |
||
| 180 | }, |
||
| 181 | }, |
||
| 182 | { |
||
| 183 | name: "Kevin", |
||
| 184 | age: 10, |
||
| 185 | location: location{ |
||
| 186 | city: "Boston", |
||
| 187 | }, |
||
| 188 | }, |
||
| 189 | } |
||
| 190 | result := gotil.SortDescBy(input, "age") |
||
| 191 | if !reflect.DeepEqual(expected, result) { |
||
| 192 | t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input, expected, result) |
||
| 193 | } |
||
| 195 |