| Conditions | 53 |
| Total Lines | 187 |
| Code Lines | 133 |
| 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 assert.compare 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 assert |
||
| 8 | func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { |
||
| 9 | switch kind { |
||
| 10 | case reflect.Int: |
||
| 11 | { |
||
| 12 | intobj1 := obj1.(int) |
||
| 13 | intobj2 := obj2.(int) |
||
| 14 | if intobj1 > intobj2 { |
||
| 15 | return -1, true |
||
| 16 | } |
||
| 17 | if intobj1 == intobj2 { |
||
| 18 | return 0, true |
||
| 19 | } |
||
| 20 | if intobj1 < intobj2 { |
||
| 21 | return 1, true |
||
| 22 | } |
||
| 23 | } |
||
| 24 | case reflect.Int8: |
||
| 25 | { |
||
| 26 | int8obj1 := obj1.(int8) |
||
| 27 | int8obj2 := obj2.(int8) |
||
| 28 | if int8obj1 > int8obj2 { |
||
| 29 | return -1, true |
||
| 30 | } |
||
| 31 | if int8obj1 == int8obj2 { |
||
| 32 | return 0, true |
||
| 33 | } |
||
| 34 | if int8obj1 < int8obj2 { |
||
| 35 | return 1, true |
||
| 36 | } |
||
| 37 | } |
||
| 38 | case reflect.Int16: |
||
| 39 | { |
||
| 40 | int16obj1 := obj1.(int16) |
||
| 41 | int16obj2 := obj2.(int16) |
||
| 42 | if int16obj1 > int16obj2 { |
||
| 43 | return -1, true |
||
| 44 | } |
||
| 45 | if int16obj1 == int16obj2 { |
||
| 46 | return 0, true |
||
| 47 | } |
||
| 48 | if int16obj1 < int16obj2 { |
||
| 49 | return 1, true |
||
| 50 | } |
||
| 51 | } |
||
| 52 | case reflect.Int32: |
||
| 53 | { |
||
| 54 | int32obj1 := obj1.(int32) |
||
| 55 | int32obj2 := obj2.(int32) |
||
| 56 | if int32obj1 > int32obj2 { |
||
| 57 | return -1, true |
||
| 58 | } |
||
| 59 | if int32obj1 == int32obj2 { |
||
| 60 | return 0, true |
||
| 61 | } |
||
| 62 | if int32obj1 < int32obj2 { |
||
| 63 | return 1, true |
||
| 64 | } |
||
| 65 | } |
||
| 66 | case reflect.Int64: |
||
| 67 | { |
||
| 68 | int64obj1 := obj1.(int64) |
||
| 69 | int64obj2 := obj2.(int64) |
||
| 70 | if int64obj1 > int64obj2 { |
||
| 71 | return -1, true |
||
| 72 | } |
||
| 73 | if int64obj1 == int64obj2 { |
||
| 74 | return 0, true |
||
| 75 | } |
||
| 76 | if int64obj1 < int64obj2 { |
||
| 77 | return 1, true |
||
| 78 | } |
||
| 79 | } |
||
| 80 | case reflect.Uint: |
||
| 81 | { |
||
| 82 | uintobj1 := obj1.(uint) |
||
| 83 | uintobj2 := obj2.(uint) |
||
| 84 | if uintobj1 > uintobj2 { |
||
| 85 | return -1, true |
||
| 86 | } |
||
| 87 | if uintobj1 == uintobj2 { |
||
| 88 | return 0, true |
||
| 89 | } |
||
| 90 | if uintobj1 < uintobj2 { |
||
| 91 | return 1, true |
||
| 92 | } |
||
| 93 | } |
||
| 94 | case reflect.Uint8: |
||
| 95 | { |
||
| 96 | uint8obj1 := obj1.(uint8) |
||
| 97 | uint8obj2 := obj2.(uint8) |
||
| 98 | if uint8obj1 > uint8obj2 { |
||
| 99 | return -1, true |
||
| 100 | } |
||
| 101 | if uint8obj1 == uint8obj2 { |
||
| 102 | return 0, true |
||
| 103 | } |
||
| 104 | if uint8obj1 < uint8obj2 { |
||
| 105 | return 1, true |
||
| 106 | } |
||
| 107 | } |
||
| 108 | case reflect.Uint16: |
||
| 109 | { |
||
| 110 | uint16obj1 := obj1.(uint16) |
||
| 111 | uint16obj2 := obj2.(uint16) |
||
| 112 | if uint16obj1 > uint16obj2 { |
||
| 113 | return -1, true |
||
| 114 | } |
||
| 115 | if uint16obj1 == uint16obj2 { |
||
| 116 | return 0, true |
||
| 117 | } |
||
| 118 | if uint16obj1 < uint16obj2 { |
||
| 119 | return 1, true |
||
| 120 | } |
||
| 121 | } |
||
| 122 | case reflect.Uint32: |
||
| 123 | { |
||
| 124 | uint32obj1 := obj1.(uint32) |
||
| 125 | uint32obj2 := obj2.(uint32) |
||
| 126 | if uint32obj1 > uint32obj2 { |
||
| 127 | return -1, true |
||
| 128 | } |
||
| 129 | if uint32obj1 == uint32obj2 { |
||
| 130 | return 0, true |
||
| 131 | } |
||
| 132 | if uint32obj1 < uint32obj2 { |
||
| 133 | return 1, true |
||
| 134 | } |
||
| 135 | } |
||
| 136 | case reflect.Uint64: |
||
| 137 | { |
||
| 138 | uint64obj1 := obj1.(uint64) |
||
| 139 | uint64obj2 := obj2.(uint64) |
||
| 140 | if uint64obj1 > uint64obj2 { |
||
| 141 | return -1, true |
||
| 142 | } |
||
| 143 | if uint64obj1 == uint64obj2 { |
||
| 144 | return 0, true |
||
| 145 | } |
||
| 146 | if uint64obj1 < uint64obj2 { |
||
| 147 | return 1, true |
||
| 148 | } |
||
| 149 | } |
||
| 150 | case reflect.Float32: |
||
| 151 | { |
||
| 152 | float32obj1 := obj1.(float32) |
||
| 153 | float32obj2 := obj2.(float32) |
||
| 154 | if float32obj1 > float32obj2 { |
||
| 155 | return -1, true |
||
| 156 | } |
||
| 157 | if float32obj1 == float32obj2 { |
||
| 158 | return 0, true |
||
| 159 | } |
||
| 160 | if float32obj1 < float32obj2 { |
||
| 161 | return 1, true |
||
| 162 | } |
||
| 163 | } |
||
| 164 | case reflect.Float64: |
||
| 165 | { |
||
| 166 | float64obj1 := obj1.(float64) |
||
| 167 | float64obj2 := obj2.(float64) |
||
| 168 | if float64obj1 > float64obj2 { |
||
| 169 | return -1, true |
||
| 170 | } |
||
| 171 | if float64obj1 == float64obj2 { |
||
| 172 | return 0, true |
||
| 173 | } |
||
| 174 | if float64obj1 < float64obj2 { |
||
| 175 | return 1, true |
||
| 176 | } |
||
| 177 | } |
||
| 178 | case reflect.String: |
||
| 179 | { |
||
| 180 | stringobj1 := obj1.(string) |
||
| 181 | stringobj2 := obj2.(string) |
||
| 182 | if stringobj1 > stringobj2 { |
||
| 183 | return -1, true |
||
| 184 | } |
||
| 185 | if stringobj1 == stringobj2 { |
||
| 186 | return 0, true |
||
| 187 | } |
||
| 188 | if stringobj1 < stringobj2 { |
||
| 189 | return 1, true |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return 0, false |
||
| 195 | } |
||
| 310 |