Conditions | 14 |
Total Lines | 51 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Tests | 26 |
CRAP Score | 14 |
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._isg.ISG._isg_i() 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 2019-2020 by Christopher C. Little. |
||
74 | def _isg_i(self, src: str, tar: str) -> float: |
||
75 | 1 | """Return an individual ISG similarity (not symmetric) for src to tar. |
|
76 | 1 | ||
77 | 1 | Parameters |
|
78 | ---------- |
||
79 | 1 | src : str |
|
80 | Source string for comparison |
||
81 | tar : str |
||
82 | Target string for comparison |
||
83 | |||
84 | Returns |
||
85 | ------- |
||
86 | float |
||
87 | The ISG similarity |
||
88 | |||
89 | |||
90 | .. versionadded:: 0.4.1 |
||
91 | |||
92 | """ |
||
93 | |||
94 | def _char_at(name: str, pos: int) -> str: |
||
95 | if pos >= len(name): |
||
96 | return '' |
||
97 | return name[pos] |
||
98 | |||
99 | 1 | matches = 0 |
|
100 | 1 | for pos in range(len(src)): |
|
101 | 1 | s = _char_at(src, pos) |
|
102 | 1 | if s and s in set(tar[max(0, pos - 1) : pos + 3]): |
|
103 | matches += 1 |
||
104 | 1 | continue |
|
105 | 1 | ||
106 | 1 | if self._full_guth: |
|
107 | 1 | t = _char_at(tar, pos) |
|
108 | 1 | if t and t in set(src[max(0, pos - 1) : pos + 3]): |
|
109 | 1 | matches += 1 |
|
110 | 1 | continue |
|
111 | |||
112 | 1 | s = _char_at(src, pos + 1) |
|
113 | 1 | t = _char_at(tar, pos + 1) |
|
114 | 1 | if s and t and s == t: |
|
115 | 1 | matches += 1 |
|
116 | 1 | continue |
|
117 | 1 | ||
118 | s = _char_at(src, pos + 2) |
||
119 | 1 | t = _char_at(tar, pos + 2) |
|
120 | 1 | if s and t and s == t: |
|
121 | 1 | matches += 1 |
|
122 | 1 | continue |
|
123 | 1 | ||
124 | return matches / (len(src) + len(tar) - matches) |
||
125 | 1 | ||
170 |