Conditions | 12 |
Total Lines | 85 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 12 |
Changes | 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 abydos.distance._sift4_extended.Sift4Extended.__init__() 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 | # Copyright 2019-2020 by Christopher C. Little. |
||
42 | def __init__( |
||
43 | self, |
||
44 | max_offset: int = 5, |
||
45 | max_distance: int = 0, |
||
46 | tokenizer: Optional[_Tokenizer] = None, |
||
47 | token_matcher: Optional[Callable[[str, str], bool]] = None, |
||
48 | matching_evaluator: Optional[Callable[[str, str], float]] = None, |
||
49 | 1 | local_length_evaluator: Optional[Callable[[float], float]] = None, |
|
50 | transposition_cost_evaluator: Optional[ |
||
51 | 1 | Callable[[int, int], float] |
|
52 | ] = None, |
||
53 | transpositions_evaluator: Optional[ |
||
54 | Callable[[float, float], float] |
||
55 | ] = None, |
||
56 | **kwargs: Any |
||
57 | ) -> None: |
||
58 | """Initialize Sift4Extended instance. |
||
59 | |||
60 | Parameters |
||
61 | ---------- |
||
62 | max_offset : int |
||
63 | The number of characters to search for matching letters |
||
64 | max_distance : int |
||
65 | The distance at which to stop and exit |
||
66 | tokenizer : _Tokenizer |
||
67 | A tokenizer instance (character tokenization by default) |
||
68 | token_matcher : function |
||
69 | A token matcher function of two parameters (equality by default). |
||
70 | :math:`Sift4Extended.sift4_token_matcher` is also supplied. |
||
71 | matching_evaluator : function |
||
72 | A token match quality function of two parameters (1 by default). |
||
73 | :math:`Sift4Extended.sift4_matching_evaluator` is also supplied. |
||
74 | local_length_evaluator : function |
||
75 | A local length evaluator function (its single parameter by |
||
76 | default). :math:`Sift4Extended.reward_length_evaluator` and |
||
77 | :math:`Sift4Extended.reward_length_evaluator_exp` are also |
||
78 | supplied. |
||
79 | transposition_cost_evaluator : function |
||
80 | A transposition cost evaluator function of two parameters (1 by |
||
81 | default). |
||
82 | :math:`Sift4Extended.longer_transpositions_are_more_costly` is also |
||
83 | supplied. |
||
84 | transpositions_evaluator : function |
||
85 | A transpositions evaluator function of two parameters (the second |
||
86 | parameter subtracted from the first, by default). |
||
87 | **kwargs |
||
88 | Arbitrary keyword arguments |
||
89 | |||
90 | |||
91 | .. versionadded:: 0.4.0 |
||
92 | |||
93 | """ |
||
94 | super(Sift4Extended, self).__init__(**kwargs) |
||
95 | self._max_offset = max_offset |
||
96 | self._max_distance = max_distance |
||
97 | |||
98 | if tokenizer is not None: |
||
99 | 1 | self._tokenizer = tokenizer |
|
100 | 1 | else: |
|
101 | 1 | self._tokenizer = CharacterTokenizer() |
|
102 | 1 | ||
103 | 1 | if token_matcher is not None: |
|
104 | 1 | self._token_matcher = token_matcher |
|
105 | 1 | else: |
|
106 | 1 | self._token_matcher = lambda t1, t2: t1 == t2 |
|
107 | 1 | ||
108 | if matching_evaluator is not None: |
||
109 | 1 | self._matching_evaluator = matching_evaluator |
|
110 | 1 | else: |
|
111 | 1 | self._matching_evaluator = lambda t1, t2: 1 |
|
112 | 1 | ||
113 | 1 | if local_length_evaluator is not None: |
|
114 | 1 | self._local_length_evaluator = local_length_evaluator |
|
115 | 1 | else: |
|
116 | 1 | self._local_length_evaluator = lambda local_cs: local_cs |
|
117 | 1 | ||
118 | 1 | if transposition_cost_evaluator is not None: |
|
119 | 1 | self._transposition_cost_evaluator = transposition_cost_evaluator |
|
120 | 1 | else: |
|
121 | self._transposition_cost_evaluator = lambda c1, c2: 1 |
||
122 | 1 | ||
123 | if transpositions_evaluator is not None: |
||
124 | self._transpositions_evaluator = transpositions_evaluator |
||
125 | else: |
||
126 | self._transpositions_evaluator = lambda lcss, trans: lcss - trans |
||
127 | |||
365 |