Conditions | 2 |
Total Lines | 66 |
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 |
||
42 | func TestSortBy(t *testing.T) { |
||
43 | // Input: [{Micheal 27 {New York}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Kevin 10 {Boston}}] |
||
44 | input := []user{ |
||
45 | { |
||
46 | name: "Micheal", |
||
47 | age: 27, |
||
48 | location: location{ |
||
49 | city: "New York", |
||
50 | }, |
||
51 | }, |
||
52 | { |
||
53 | name: "Joe", |
||
54 | age: 30, |
||
55 | location: location{ |
||
56 | city: "Detroit", |
||
57 | }, |
||
58 | }, |
||
59 | { |
||
60 | name: "Olivia", |
||
61 | age: 42, |
||
62 | location: location{ |
||
63 | city: "New York", |
||
64 | }, |
||
65 | }, |
||
66 | { |
||
67 | name: "Kevin", |
||
68 | age: 10, |
||
69 | location: location{ |
||
70 | city: "Boston", |
||
71 | }, |
||
72 | }, |
||
73 | } |
||
74 | expected := []user{ |
||
75 | { |
||
76 | name: "Kevin", |
||
77 | age: 10, |
||
78 | location: location{ |
||
79 | city: "Boston", |
||
80 | }, |
||
81 | }, |
||
82 | |||
83 | { |
||
84 | name: "Joe", |
||
85 | age: 30, |
||
86 | location: location{ |
||
87 | city: "Detroit", |
||
88 | }, |
||
89 | }, |
||
90 | { |
||
91 | name: "Olivia", |
||
92 | age: 42, |
||
93 | location: location{ |
||
94 | city: "New York", |
||
95 | }, |
||
96 | }, |
||
97 | { |
||
98 | name: "Micheal", |
||
99 | age: 27, |
||
100 | location: location{ |
||
101 | city: "New York", |
||
102 | }, |
||
103 | }, |
||
104 | } |
||
105 | result := gotil.SortBy(input, "location.city") |
||
106 | if !reflect.DeepEqual(expected, result) { |
||
107 | t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input, expected, result) |
||
108 | } |
||
195 |