Conditions | 13 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 test_double_nested_config() 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 python |
||
198 | def test_double_nested_config(): |
||
199 | sub_sub_ing = Ingredient('sub_sub_ing') |
||
200 | sub_ing = Ingredient('sub_ing', [sub_sub_ing]) |
||
201 | ing = Ingredient('ing', [sub_ing]) |
||
202 | ex = Experiment('ex', [ing]) |
||
203 | |||
204 | @ex.config |
||
205 | def config(): |
||
206 | a = 1 |
||
207 | |||
208 | @ing.config |
||
209 | def config(): |
||
210 | b = 1 |
||
211 | |||
212 | @sub_ing.config |
||
213 | def config(): |
||
214 | c = 2 |
||
215 | |||
216 | @sub_sub_ing.config |
||
217 | def config(): |
||
218 | d = 3 |
||
219 | |||
220 | @sub_sub_ing.capture |
||
221 | def sub_sub_ing_main(_config): |
||
222 | assert _config == { |
||
223 | 'd': 3 |
||
224 | }, _config |
||
225 | |||
226 | @sub_ing.capture |
||
227 | def sub_ing_main(_config): |
||
228 | assert _config == { |
||
229 | 'c': 2, |
||
230 | 'sub_sub_ing': {'d': 3} |
||
231 | }, _config |
||
232 | |||
233 | @ing.capture |
||
234 | def ing_main(_config): |
||
235 | assert _config == { |
||
236 | 'b': 1, |
||
237 | 'sub_sub_ing': {'d': 3}, |
||
238 | 'sub_ing': {'c': 2} |
||
239 | }, _config |
||
240 | |||
241 | @ex.main |
||
242 | def main(_config): |
||
243 | _config.pop('seed') |
||
244 | assert _config == { |
||
245 | 'a': 1, |
||
246 | 'sub_sub_ing': {'d': 3}, |
||
247 | 'sub_ing': {'c': 2}, |
||
248 | 'ing': {'b': 1} |
||
249 | }, _config |
||
250 | |||
251 | ing_main() |
||
252 | sub_ing_main() |
||
253 | sub_sub_ing_main() |
||
254 | |||
255 | ex.run() |
||
256 |