Conditions | 20 |
Total Lines | 53 |
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 dumpAST() 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 | from __future__ import absolute_import, unicode_literals |
||
56 | def dumpAST(obj, ind=0, topnode=False): |
||
57 | """Print out a block/entire AST.""" |
||
58 | indChar = ("\t" * ind) + "-> " if ind else "" |
||
59 | print(indChar + "[" + obj.t + "]") |
||
60 | if not obj.title == "": |
||
61 | print("\t" + indChar + "Title: " + (obj.title or '')) |
||
62 | if not obj.info == "": |
||
63 | print("\t" + indChar + "Info: " + (obj.info or '')) |
||
64 | if not obj.destination == "": |
||
65 | print("\t" + indChar + "Destination: " + (obj.destination or '')) |
||
66 | if obj.is_open: |
||
67 | print("\t" + indChar + "Open: " + str(obj.is_open)) |
||
68 | if obj.last_line_blank: |
||
69 | print( |
||
70 | "\t" + indChar + "Last line blank: " + str(obj.last_line_blank)) |
||
71 | if obj.sourcepos: |
||
72 | print("\t" + indChar + "Sourcepos: " + str(obj.sourcepos)) |
||
73 | if not obj.string_content == "": |
||
74 | print("\t" + indChar + "String content: " + (obj.string_content or '')) |
||
75 | if not obj.info == "": |
||
76 | print("\t" + indChar + "Info: " + (obj.info or '')) |
||
77 | if not obj.literal == "": |
||
78 | print("\t" + indChar + "Literal: " + (obj.literal or '')) |
||
79 | if obj.list_data.get('type'): |
||
80 | print("\t" + indChar + "List Data: ") |
||
81 | print("\t\t" + indChar + "[type] = " + obj.list_data.get('type')) |
||
82 | if obj.list_data.get('bullet_char'): |
||
83 | print( |
||
84 | "\t\t" + indChar + "[bullet_char] = " + |
||
85 | obj.list_data['bullet_char']) |
||
86 | if obj.list_data.get('start'): |
||
87 | print( |
||
88 | "\t\t" + indChar + "[start] = " + |
||
89 | str(obj.list_data.get('start'))) |
||
90 | if obj.list_data.get('delimiter'): |
||
91 | print( |
||
92 | "\t\t" + indChar + "[delimiter] = " + |
||
93 | obj.list_data.get('delimiter')) |
||
94 | if obj.list_data.get('padding'): |
||
95 | print( |
||
96 | "\t\t" + indChar + "[padding] = " + |
||
97 | str(obj.list_data.get('padding'))) |
||
98 | if obj.list_data.get('marker_offset'): |
||
99 | print( |
||
100 | "\t\t" + indChar + "[marker_offset] = " + |
||
101 | str(obj.list_data.get('marker_offset'))) |
||
102 | if obj.walker: |
||
103 | print("\t" + indChar + "Children:") |
||
104 | walker = obj.walker() |
||
105 | nxt = walker.nxt() |
||
106 | while nxt is not None and topnode is False: |
||
107 | dumpAST(nxt['node'], ind + 2, topnode=True) |
||
108 | nxt = walker.nxt() |
||
109 |