| Conditions | 57 |
| Total Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 3 | 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 Utility.get_benchmark() 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 | """Implementation of benchmarks utility function.""" |
||
| 17 | def get_benchmark(self, benchmark, Lower=None, Upper=None): |
||
| 18 | if not isinstance(benchmark, ''.__class__): |
||
| 19 | return benchmark |
||
| 20 | else: |
||
| 21 | if benchmark == 'rastrigin': |
||
| 22 | if Lower is None and Upper is None: |
||
| 23 | return Rastrigin() |
||
| 24 | elif Lower is not None and Upper is not None: |
||
| 25 | return Rastrigin(Lower, Upper) |
||
| 26 | else: |
||
| 27 | self.__raiseLowerAndUpperNotDefined() |
||
| 28 | elif benchmark == 'rosenbrock': |
||
| 29 | if Lower is None and Upper is None: |
||
| 30 | return Rosenbrock() |
||
| 31 | elif Lower is not None and Upper is not None: |
||
| 32 | return Rosenbrock(Lower, Upper) |
||
| 33 | else: |
||
| 34 | self.__raiseLowerAndUpperNotDefined() |
||
| 35 | elif benchmark == 'griewank': |
||
| 36 | if Lower is None and Upper is None: |
||
| 37 | return Griewank() |
||
| 38 | elif Lower is not None and Upper != None: |
||
| 39 | return Griewank(Lower, Upper) |
||
| 40 | else: |
||
| 41 | self.__raiseLowerAndUpperNotDefined() |
||
| 42 | elif benchmark == 'sphere': |
||
| 43 | if Lower is None and Upper is None: |
||
| 44 | return Sphere() |
||
| 45 | elif Lower is not None and Upper is not None: |
||
| 46 | return Sphere(Lower, Upper) |
||
| 47 | else: |
||
| 48 | self.__raiseLowerAndUpperNotDefined() |
||
| 49 | elif benchmark == 'ackley': |
||
| 50 | if Lower is None and Upper is None: |
||
| 51 | return Ackley() |
||
| 52 | elif Lower is not None and Upper is not None: |
||
| 53 | return Ackley(Lower, Upper) |
||
| 54 | else: |
||
| 55 | self.__raiseLowerAndUpperNotDefined() |
||
| 56 | elif benchmark == 'schwefel': |
||
| 57 | if Lower is None and Upper is None: |
||
| 58 | return Schwefel() |
||
| 59 | elif Lower is not None and Upper is not None: |
||
| 60 | return Schwefel(Lower, Upper) |
||
| 61 | else: |
||
| 62 | self.__raiseLowerAndUpperNotDefined() |
||
| 63 | elif benchmark == 'schwefel221': |
||
| 64 | if Lower is None and Upper is None: |
||
| 65 | return Schwefel221() |
||
| 66 | elif Lower is not None and Upper is not None: |
||
| 67 | return Schwefel221(Lower, Upper) |
||
| 68 | else: |
||
| 69 | self.__raiseLowerAndUpperNotDefined() |
||
| 70 | elif benchmark == 'schwefel222': |
||
| 71 | if Lower is None and Upper is None: |
||
| 72 | return Schwefel222() |
||
| 73 | elif Lower is not None and Upper is not None: |
||
| 74 | return Schwefel222(Lower, Upper) |
||
| 75 | else: |
||
| 76 | self.__raiseLowerAndUpperNotDefined() |
||
| 77 | elif benchmark == 'whitley': |
||
| 78 | if Lower is None and Upper is None: |
||
| 79 | return Whitley() |
||
| 80 | elif Lower is not None and Upper is not None: |
||
| 81 | return Whitley(Lower, Upper) |
||
| 82 | else: |
||
| 83 | self.__raiseLowerAndUpperNotDefined() |
||
| 84 | elif benchmark == 'alpine1': |
||
| 85 | if Lower is None and Upper is None: |
||
| 86 | return Alpine1() |
||
| 87 | elif Lower is not None and Upper is not None: |
||
| 88 | return Alpine1(Lower, Upper) |
||
| 89 | else: |
||
| 90 | self.__raiseLowerAndUpperNotDefined() |
||
| 91 | elif benchmark == 'alpine2': |
||
| 92 | if Lower is None and Upper is None: |
||
| 93 | return Alpine2() |
||
| 94 | elif Lower is not None and Upper is not None: |
||
| 95 | return Alpine2(Lower, Upper) |
||
| 96 | else: |
||
| 97 | self.__raiseLowerAndUpperNotDefined() |
||
| 98 | else: |
||
| 99 | raise TypeError('Passed benchmark is not defined!') |
||
| 100 | |||
| 104 |