Conditions | 10 |
Total Lines | 57 |
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.generalize() 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 |
||
25 | def generalize(self) -> None: |
||
26 | """ |
||
27 | Remove all parts of the strings that are specific to just this payment, |
||
28 | in order to have a string that is the same across different payments of |
||
29 | the same type (e.g. a monthly payment to Apple for iCloud storage). |
||
30 | |||
31 | :return: |
||
32 | """ |
||
33 | parts: List[str] = self.text.split() |
||
34 | |||
35 | pattern = compile_regex(r'\*\d{4}') |
||
36 | |||
37 | try: |
||
38 | if pattern.match(parts[0]): |
||
39 | del parts[0] |
||
40 | except IndexError: |
||
41 | return |
||
42 | |||
43 | pattern = compile_regex(r'\d{2}\.\d{2}') |
||
44 | |||
45 | if pattern.match(parts[0]): |
||
46 | del parts[0] |
||
47 | |||
48 | currency: Any = iso4217parse.by_alpha3(parts[0]) |
||
49 | |||
50 | if isinstance(currency, iso4217parse.Currency): |
||
51 | del parts[0] |
||
52 | del parts[0] |
||
53 | |||
54 | pattern = compile_regex(r'\d{1}\.\d{4}') |
||
55 | |||
56 | if pattern.match(parts[-1]): |
||
57 | del parts[-1] |
||
58 | del parts[-1] |
||
59 | |||
60 | pattern = compile_regex(r'\d{2}\.\d{2}\.\d{2}') |
||
61 | |||
62 | if pattern.match(parts[-1]): |
||
63 | del parts[-1] |
||
64 | |||
65 | pattern = compile_regex(r'.+:') |
||
66 | |||
67 | if pattern.match(parts[-1]): |
||
68 | del parts[-1] |
||
69 | |||
70 | pattern = compile_regex(r'\d+,\d{2}') |
||
71 | |||
72 | if pattern.match(parts[-1]): |
||
73 | del parts[-1] |
||
74 | |||
75 | currency: Any = iso4217parse.by_alpha3(parts[-1]) |
||
76 | if isinstance(currency, iso4217parse.Currency): |
||
77 | del parts[-1] |
||
78 | |||
79 | text = ' '.join(parts) |
||
80 | |||
81 | self.text = text |
||
82 |