Conditions | 11 |
Total Lines | 47 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like hyperactive.optimizers.strategies.optimizer_attributes.OptimizerAttributes._add_result_attributes() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # Author: Simon Blanke |
||
18 | def _add_result_attributes( |
||
19 | self, |
||
20 | best_para, |
||
21 | best_score, |
||
22 | best_since_iter, |
||
23 | eval_times, |
||
24 | iter_times, |
||
25 | search_data, |
||
26 | random_seed, |
||
27 | ): |
||
28 | if self.best_para is None: |
||
29 | self.best_para = best_para |
||
30 | else: |
||
31 | if best_score > self.best_score: |
||
32 | self.best_para = best_para |
||
33 | |||
34 | if self.best_score is None: |
||
35 | self.best_score = best_score |
||
36 | else: |
||
37 | if best_score > self.best_score: |
||
38 | self.best_score = best_score |
||
39 | |||
40 | if self.best_since_iter is None: |
||
41 | self.best_since_iter = best_since_iter |
||
42 | else: |
||
43 | if best_score > self.best_score: |
||
44 | self.best_since_iter = best_since_iter |
||
45 | |||
46 | if self.eval_times is None: |
||
47 | self.eval_times = eval_times |
||
48 | else: |
||
49 | self.eval_times = self.eval_times + eval_times |
||
50 | |||
51 | if self.iter_times is None: |
||
52 | self.iter_times = iter_times |
||
53 | else: |
||
54 | self.iter_times = self.iter_times + eval_times |
||
55 | |||
56 | if self.search_data is None: |
||
57 | self.search_data = search_data |
||
58 | else: |
||
59 | self.search_data = pd.concat( |
||
60 | [self.search_data, search_data], ignore_index=True |
||
61 | ) |
||
62 | |||
63 | if self.random_seed is None: |
||
64 | self.random_seed = random_seed |
||
65 |