Conditions | 17 |
Total Lines | 56 |
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 _test_group() 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 | # Copyright (c) 2014, Salesforce.com, Inc. All rights reserved. |
||
61 | def _test_group(name): |
||
62 | modules = MODULES[name] |
||
63 | EXAMPLES = [e for m in modules for e in m.EXAMPLES] |
||
64 | for EXAMPLE in EXAMPLES: |
||
65 | values = EXAMPLE['values'][:] |
||
66 | raw_shared = EXAMPLE['shared'] |
||
67 | temp_shared = modules[0].Shared.from_dict(raw_shared) |
||
68 | for value in values: |
||
69 | temp_shared.add_value(value) |
||
70 | temp_shared.realize() |
||
71 | raw_shared = temp_shared.dump() |
||
72 | |||
73 | shareds = [module.Shared.from_dict(raw_shared) for module in modules] |
||
74 | groups = [ |
||
75 | module.Group.from_values(shared) |
||
76 | for module, shared in zip(modules, shareds)] |
||
77 | modules_shareds_groups = zip(modules, shareds, groups) |
||
78 | |||
79 | for value in values: |
||
80 | for module, shared, group in modules_shareds_groups: |
||
81 | group.add_value(shared, value) |
||
82 | dumped = [g.dump() for g in groups] |
||
83 | assert_all_close(dumped, err_msg='group_dump') |
||
84 | |||
85 | for module, shared, group in modules_shareds_groups: |
||
86 | value = group.sample_value(shared) |
||
87 | shared.add_value(value) |
||
88 | values.append(value) |
||
89 | |||
90 | for value in values: |
||
91 | scores = [ |
||
92 | group.score_value(shared, value) |
||
93 | for module, shared, group in modules_shareds_groups |
||
94 | ] |
||
95 | for module, shared, group in modules_shareds_groups: |
||
96 | print "------------------" |
||
97 | print module |
||
98 | print shared.dump() |
||
99 | print group.dump() |
||
100 | print value |
||
101 | assert_all_close(scores, err_msg='score_value') |
||
102 | |||
103 | scores = [ |
||
104 | group.score_data(shared) |
||
105 | for module, shared, group in modules_shareds_groups] |
||
106 | assert_all_close(scores, err_msg='score_data') |
||
107 | |||
108 | for module, shared, group in modules_shareds_groups: |
||
109 | dumped = group.dump() |
||
110 | group.init(shared) |
||
111 | group.load(dumped) |
||
112 | |||
113 | scores = [ |
||
114 | group.score_data(shared) |
||
115 | for module, shared, group in modules_shareds_groups] |
||
116 | assert_all_close(scores, err_msg='score_data') |
||
117 | |||
137 |