Conditions | 13 |
Total Lines | 63 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Tests | 29 |
CRAP Score | 13 |
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._inclusion.Inclusion.dist() 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 | # -*- coding: utf-8 -*- |
||
59 | 1 | def dist(self, src, tar): |
|
60 | """Return the INClusion Programme value of two words. |
||
61 | |||
62 | Parameters |
||
63 | ---------- |
||
64 | src : str |
||
65 | Source string for comparison |
||
66 | tar : str |
||
67 | Target string for comparison |
||
68 | |||
69 | Returns |
||
70 | ------- |
||
71 | float |
||
72 | The INC Programme distance |
||
73 | |||
74 | Examples |
||
75 | -------- |
||
76 | >>> cmp = Inclusion() |
||
77 | >>> round(cmp.dist('cat', 'hat'), 12) |
||
78 | 1.0 |
||
79 | >>> round(cmp.dist('Niall', 'Neil'), 12) |
||
80 | 1.0 |
||
81 | >>> cmp.dist('aluminum', 'Catalan') |
||
82 | 1.0 |
||
83 | >>> cmp.dist('ATCG', 'TAGC') |
||
84 | 1.0 |
||
85 | |||
86 | |||
87 | .. versionadded:: 0.4.1 |
||
88 | |||
89 | """ |
||
90 | 1 | if src == tar: |
|
91 | 1 | return 0.0 |
|
92 | 1 | if len(src) == len(tar): |
|
93 | 1 | return 1.0 |
|
94 | |||
95 | 1 | diff, src, tar = self._lev.alignment(src, tar) |
|
96 | |||
97 | 1 | src = list(src) |
|
98 | 1 | tar = list(tar) |
|
99 | |||
100 | 1 | while src and src[0] == '-': |
|
101 | 1 | src.pop(0) |
|
102 | 1 | tar.pop(0) |
|
103 | 1 | diff -= 1 |
|
104 | 1 | while tar and tar[0] == '-': |
|
105 | 1 | src.pop(0) |
|
106 | 1 | tar.pop(0) |
|
107 | 1 | diff -= 1 |
|
108 | 1 | while src and src[-1] == '-': |
|
109 | 1 | src.pop(0) |
|
110 | 1 | tar.pop(0) |
|
111 | 1 | diff -= 1 |
|
112 | 1 | while tar and tar[-1] == '-': |
|
113 | 1 | src.pop(0) |
|
114 | 1 | tar.pop(0) |
|
115 | 1 | diff -= 1 |
|
116 | |||
117 | 1 | if diff > 1: |
|
118 | 1 | return 1.0 |
|
119 | 1 | if len(src) - diff < 3: |
|
120 | 1 | return 1.0 |
|
121 | 1 | return 0.0 |
|
122 | |||
128 |