Conditions | 17 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 DefListProcessor.run() 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 | """ |
||
35 | def run(self, parent, blocks): |
||
36 | |||
37 | raw_block = blocks.pop(0) |
||
38 | m = self.RE.search(raw_block) |
||
39 | terms = [l.strip() for l in |
||
40 | raw_block[:m.start()].split('\n') if l.strip()] |
||
41 | block = raw_block[m.end():] |
||
42 | no_indent = self.NO_INDENT_RE.match(block) |
||
43 | if no_indent: |
||
44 | d, theRest = (block, None) |
||
45 | else: |
||
46 | d, theRest = self.detab(block) |
||
47 | if d: |
||
48 | d = '%s\n%s' % (m.group(2), d) |
||
49 | else: |
||
50 | d = m.group(2) |
||
51 | sibling = self.lastChild(parent) |
||
52 | if not terms and sibling is None: |
||
53 | # This is not a definition item. Most likely a paragraph that |
||
54 | # starts with a colon at the begining of a document or list. |
||
55 | blocks.insert(0, raw_block) |
||
56 | return False |
||
57 | if not terms and sibling.tag == 'p': |
||
58 | # The previous paragraph contains the terms |
||
59 | state = 'looselist' |
||
60 | terms = sibling.text.split('\n') |
||
61 | parent.remove(sibling) |
||
62 | # Aquire new sibling |
||
63 | sibling = self.lastChild(parent) |
||
64 | else: |
||
65 | state = 'list' |
||
66 | |||
67 | if sibling is not None and sibling.tag == 'dl': |
||
68 | # This is another item on an existing list |
||
69 | dl = sibling |
||
70 | if not terms and len(dl) and dl[-1].tag == 'dd' and len(dl[-1]): |
||
71 | state = 'looselist' |
||
72 | else: |
||
73 | # This is a new list |
||
74 | dl = etree.SubElement(parent, 'dl') |
||
75 | # Add terms |
||
76 | for term in terms: |
||
77 | dt = etree.SubElement(dl, 'dt') |
||
78 | dt.text = term |
||
79 | # Add definition |
||
80 | self.parser.state.set(state) |
||
81 | dd = etree.SubElement(dl, 'dd') |
||
82 | self.parser.parseBlocks(dd, [d]) |
||
83 | self.parser.state.reset() |
||
84 | |||
85 | if theRest: |
||
86 | blocks.insert(0, theRest) |
||
87 | |||
116 |