Conditions | 16 |
Total Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 TestStarStruct.test_verifying_unpack() 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 | #!/usr/bin/env python3 |
||
201 | def test_verifying_unpack(self): |
||
202 | def adder(*args): |
||
203 | return sum(args) |
||
204 | |||
205 | AdderMessage = Message('AdderMessage', [ |
||
206 | ('item_a', 'H'), |
||
207 | ('item_b', 'B'), |
||
208 | ('item_c', 'B'), |
||
209 | ('item_d', 'B'), |
||
210 | ('item_e', 'B'), |
||
211 | # Note, there is no item 'e' in the list of arguments |
||
212 | ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d']), |
||
213 | ]) |
||
214 | |||
215 | # Test getting the correct result |
||
216 | test_data = { |
||
217 | 'item_a': 2, |
||
218 | 'item_b': 5, |
||
219 | 'item_c': 7, |
||
220 | 'item_d': 4, |
||
221 | 'item_e': 6, |
||
222 | } |
||
223 | |||
224 | made = AdderMessage.make(test_data) |
||
225 | assert made.item_a == 2 |
||
226 | assert made.item_b == 5 |
||
227 | |||
228 | assert made.function_data == 2 + 5 + 7 + 4 |
||
229 | |||
230 | # Check packing and unpacking |
||
231 | packed = AdderMessage.pack(test_data) |
||
232 | assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' |
||
233 | assert packed == made.pack() |
||
234 | |||
235 | unpacked = AdderMessage.unpack(packed) |
||
236 | assert made == unpacked |
||
237 | |||
238 | # Now we modify the data we are going to unpack, and we should get an error |
||
239 | modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' |
||
240 | |||
241 | with pytest.raises(ValueError): |
||
242 | unpacked = AdderMessage.unpack(modified_packed) |
||
243 | |||
244 | AdderMessageFalse = Message('AdderMessageFalse', [ |
||
245 | ('item_a', 'H'), |
||
246 | ('item_b', 'B'), |
||
247 | ('item_c', 'B'), |
||
248 | ('item_d', 'B'), |
||
249 | ('item_e', 'B'), |
||
250 | # Note, there is no item 'e' in the list of arguments |
||
251 | ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d'], False), |
||
252 | ]) |
||
253 | |||
254 | # Test getting the correct result |
||
255 | test_data = { |
||
256 | 'item_a': 2, |
||
257 | 'item_b': 5, |
||
258 | 'item_c': 7, |
||
259 | 'item_d': 4, |
||
260 | 'item_e': 6, |
||
261 | } |
||
262 | |||
263 | made = AdderMessageFalse.make(test_data) |
||
264 | assert made.item_a == 2 |
||
265 | assert made.item_b == 5 |
||
266 | |||
267 | assert made.function_data == 2 + 5 + 7 + 4 |
||
268 | |||
269 | # Check packing and unpacking |
||
270 | packed = AdderMessageFalse.pack(test_data) |
||
271 | assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' |
||
272 | assert packed == made.pack() |
||
273 | |||
274 | unpacked = AdderMessageFalse.unpack(packed) |
||
275 | assert made == unpacked |
||
276 | |||
277 | # Now we modify the data we are going to unpack, and we should get an error |
||
278 | modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' |
||
279 | |||
280 | # This time it won't fail because we set False for this message |
||
281 | unpacked = AdderMessageFalse.unpack(modified_packed) |
||
282 | assert unpacked.item_a == 2 |
||
283 |
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example
could be written as