Conditions | 13 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 collect() 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 | #!/usr/bin/env python |
||
50 | def collect(key, value, format, meta): |
||
51 | global headers |
||
52 | |||
53 | # Is it a header? Keep the correct numbered headers in the headers array |
||
54 | if key == 'Header': |
||
55 | [level, [id, classes, attributes], content] = value |
||
56 | if 'unnumbered' not in classes: |
||
57 | headers[level - 1] = headers[level - 1] + 1 |
||
58 | for index in range(level, 6): |
||
59 | headers[index] = 0 |
||
60 | |||
61 | # Is it a link with a right tag? |
||
62 | elif key == 'Span': |
||
63 | |||
64 | # Get the Span |
||
65 | [[anchor, classes, other], text] = value |
||
66 | |||
67 | # Is the anchor correct? |
||
68 | result = re.match('^([a-zA-Z][\w.-]*):([\w.-]+)$', anchor) |
||
69 | if result: |
||
70 | global collections |
||
71 | |||
72 | # Compute the name |
||
73 | name = result.group(1) |
||
74 | |||
75 | # Compute the identifier |
||
76 | identifier = result.group(2) |
||
77 | |||
78 | # Store the new item |
||
79 | string = stringify(deepcopy(text), format) |
||
80 | |||
81 | # Prepare the names |
||
82 | names = [] |
||
83 | |||
84 | # Add the atomic name to the list |
||
85 | names.append(name) |
||
86 | |||
87 | # Prepare the latex output |
||
88 | if format == 'latex': |
||
89 | latex = '\\phantomsection\\addcontentsline{' + name + '}{figure}{' + string + '}' |
||
90 | |||
91 | # Loop on all the headers |
||
92 | for i in [0, 1, 2, 3, 4, 5]: |
||
93 | if headers[i] > 0: |
||
94 | # Add an alternate name to the list |
||
95 | altName = name + ':' + '.'.join(map(str, headers[:i+1])) |
||
96 | names.append(altName) |
||
97 | if format == 'latex': |
||
98 | # Complete the latex output |
||
99 | latex = latex + '\\phantomsection\\addcontentsline{' + altName + '}{figure}{' + string + '}' |
||
100 | latex = latex + '\\phantomsection\\addcontentsline{' + altName + '_}{figure}{' + string + '}' |
||
101 | else: |
||
102 | break |
||
103 | |||
104 | for name in names: |
||
105 | # Prepare the new collections if needed |
||
106 | if name not in collections: |
||
107 | collections[name] = [] |
||
108 | collections[name].append({'identifier': identifier, 'text': string}) |
||
109 | |||
110 | # Special case for LaTeX output |
||
111 | if format == 'latex': |
||
112 | text.insert(0, RawInline('tex', latex)) |
||
113 | value[1] = text |
||
114 | |||
195 |