Conditions | 10 |
Total Lines | 52 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
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 paytext.paytext.PaymentText.clean() 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 | # pyre-strict |
||
19 | def clean(self) -> None: |
||
1 ignored issue
–
show
|
|||
20 | parts: List[str] = self.text.split() |
||
1 ignored issue
–
show
|
|||
21 | |||
22 | # pprint(parts) |
||
23 | |||
24 | pattern = compile('\*\d{4}') |
||
1 ignored issue
–
show
|
|||
25 | |||
26 | try: |
||
1 ignored issue
–
show
|
|||
27 | if pattern.match(parts[0]): |
||
2 ignored issues
–
show
|
|||
28 | del parts[0] |
||
1 ignored issue
–
show
|
|||
29 | except IndexError: |
||
1 ignored issue
–
show
|
|||
30 | return |
||
1 ignored issue
–
show
|
|||
31 | |||
32 | pattern = compile('\d{2}\.\d{2}') |
||
1 ignored issue
–
show
|
|||
33 | |||
34 | if pattern.match(parts[0]): |
||
1 ignored issue
–
show
|
|||
35 | del parts[0] |
||
1 ignored issue
–
show
|
|||
36 | |||
37 | currency: Any = iso4217parse.by_alpha3(parts[0]) |
||
1 ignored issue
–
show
|
|||
38 | |||
39 | if isinstance(currency, iso4217parse.Currency): |
||
2 ignored issues
–
show
|
|||
40 | del parts[0] |
||
1 ignored issue
–
show
|
|||
41 | del parts[0] |
||
1 ignored issue
–
show
|
|||
42 | |||
43 | pattern = compile('\d{1}\.\d{4}') |
||
1 ignored issue
–
show
|
|||
44 | |||
45 | if pattern.match(parts[-1]): |
||
1 ignored issue
–
show
|
|||
46 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
47 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
48 | |||
49 | pattern = compile('\d{2}\.\d{2}\.\d{2}') |
||
1 ignored issue
–
show
|
|||
50 | |||
51 | if pattern.match(parts[-1]): |
||
1 ignored issue
–
show
|
|||
52 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
53 | |||
54 | pattern = compile('.+:') |
||
1 ignored issue
–
show
|
|||
55 | |||
56 | if pattern.match(parts[-1]): |
||
1 ignored issue
–
show
|
|||
57 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
58 | |||
59 | pattern = compile('\d+,\d{2}') |
||
1 ignored issue
–
show
|
|||
60 | |||
61 | if pattern.match(parts[-1]): |
||
1 ignored issue
–
show
|
|||
62 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
63 | |||
64 | currency: Any = iso4217parse.by_alpha3(parts[-1]) |
||
1 ignored issue
–
show
|
|||
65 | if isinstance(currency, iso4217parse.Currency): |
||
1 ignored issue
–
show
|
|||
66 | del parts[-1] |
||
1 ignored issue
–
show
|
|||
67 | |||
68 | text = ' '.join(parts) |
||
1 ignored issue
–
show
|
|||
69 | |||
70 | self.text = text |
||
1 ignored issue
–
show
|
|||
71 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.