| Conditions | 12 | 
| Total Lines | 64 | 
| 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_adding_element_list() 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 | ||
| 109 | def test_adding_element_list(self): | ||
| 110 | def adder(*args): | ||
| 111 | return sum(args) | ||
| 112 | |||
| 113 |         AdderMessage = Message('AdderMessage', [ | ||
| 114 |             ('item_a', 'H'), | ||
| 115 |             ('item_b', 'B'), | ||
| 116 |             ('item_c', 'B'), | ||
| 117 |             ('item_d', 'B'), | ||
| 118 |             ('item_e', 'B'), | ||
| 119 | # Note, there is no item 'e' in the list of arguments | ||
| 120 |             ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d']), | ||
| 121 | ]) | ||
| 122 | |||
| 123 | # Test getting the correct result | ||
| 124 |         test_data = { | ||
| 125 | 'item_a': 2, | ||
| 126 | 'item_b': 5, | ||
| 127 | 'item_c': 7, | ||
| 128 | 'item_d': 4, | ||
| 129 | 'item_e': 6, | ||
| 130 | } | ||
| 131 | |||
| 132 | made = AdderMessage.make(test_data) | ||
| 133 | assert made.item_a == 2 | ||
| 134 | assert made.item_b == 5 | ||
| 135 | |||
| 136 | assert made.function_data == 2 + 5 + 7 + 4 | ||
| 137 | |||
| 138 | # Check packing and unpacking | ||
| 139 | packed = AdderMessage.pack(test_data) | ||
| 140 | assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' | ||
| 141 | assert packed == made.pack() | ||
| 142 | |||
| 143 | unpacked = AdderMessage.unpack(packed) | ||
| 144 | assert made == unpacked | ||
| 145 | |||
| 146 | # Test with correct result | ||
| 147 |         test_data_2 = { | ||
| 148 | 'item_a': 2, | ||
| 149 | 'item_b': 5, | ||
| 150 | 'item_c': 7, | ||
| 151 | 'item_d': 4, | ||
| 152 | 'item_e': 6, | ||
| 153 | 'function_data': 2 + 5 + 7 + 4, | ||
| 154 | } | ||
| 155 | made = AdderMessage.make(test_data_2) | ||
| 156 | assert made.item_a == 2 | ||
| 157 | assert made.item_b == 5 | ||
| 158 | |||
| 159 | assert made.function_data == 2 + 5 + 7 + 4 | ||
| 160 | |||
| 161 | # Test with incorrect result | ||
| 162 |         test_data_2 = { | ||
| 163 | 'item_a': 2, | ||
| 164 | 'item_b': 5, | ||
| 165 | 'item_c': 7, | ||
| 166 | 'item_d': 4, | ||
| 167 | 'item_e': 6, | ||
| 168 | 'function_data': -1, | ||
| 169 | } | ||
| 170 | |||
| 171 | with pytest.raises(ValueError): | ||
| 172 | made = AdderMessage.make(test_data_2) | ||
| 173 | |||
| 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