Conditions | 19 |
Total Lines | 66 |
Code Lines | 51 |
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 exabgp.configuration.static.attributes() 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 | # encoding: utf-8 |
||
107 | @ParseStatic.register('attributes', 'append-route') |
||
108 | def attributes(tokeniser): |
||
109 | action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW |
||
110 | ipmask = prefix(lambda: tokeniser.tokens[-1]) |
||
111 | tokeniser.afi = ipmask.afi |
||
112 | |||
113 | if 'rd' in tokeniser.tokens or 'route-distinguisher' in tokeniser.tokens: |
||
114 | nlri = IPVPN(IP.toafi(ipmask.top()), SAFI.mpls_vpn, action) |
||
115 | elif 'label' in tokeniser.tokens: |
||
116 | nlri = Label(IP.toafi(ipmask.top()), SAFI.nlri_mpls, action) |
||
117 | else: |
||
118 | nlri = INET(IP.toafi(ipmask.top()), IP.tosafi(ipmask.top()), action) |
||
119 | |||
120 | nlri.cidr = CIDR(ipmask.pack(), ipmask.mask) |
||
121 | attr = Attributes() |
||
122 | |||
123 | labels = None |
||
124 | rd = None |
||
125 | |||
126 | while True: |
||
127 | command = tokeniser() |
||
128 | |||
129 | if not command: |
||
130 | return [] |
||
131 | |||
132 | if command == 'nlri': |
||
133 | break |
||
134 | |||
135 | if command == 'label': |
||
136 | labels = label(tokeniser) |
||
137 | continue |
||
138 | |||
139 | if command == 'rd' or command == 'route-distinguisher': |
||
140 | rd = route_distinguisher(tokeniser) |
||
141 | continue |
||
142 | |||
143 | action = ParseStatic.action[command] |
||
144 | |||
145 | if action == 'attribute-add': |
||
146 | attr.add(ParseStatic.known[command](tokeniser)) |
||
147 | elif action == 'nlri-set': |
||
148 | nlri.assign(ParseStatic.assign[command], ParseStatic.known[command](tokeniser)) |
||
149 | elif action == 'nexthop-and-attribute': |
||
150 | nexthop, attribute = ParseStatic.known[command](tokeniser) |
||
151 | nlri.nexthop = nexthop |
||
152 | attr.add(attribute) |
||
153 | else: |
||
154 | raise ValueError('unknown command "%s"' % command) |
||
155 | |||
156 | changes = [] |
||
157 | while True: |
||
158 | peeked_nlri = tokeniser.peek() |
||
159 | if not peeked_nlri: |
||
160 | break |
||
161 | |||
162 | ipmask = prefix(tokeniser) |
||
163 | new = Change(nlri.__class__(nlri.afi, nlri.safi, OUT.UNSET), attr) |
||
164 | new.nlri.cidr = CIDR(ipmask.pack(), ipmask.mask) |
||
165 | if labels: |
||
166 | new.nlri.labels = labels |
||
167 | if rd: |
||
168 | new.nlri.rd = rd |
||
169 | new.nlri.nexthop = nlri.nexthop |
||
170 | changes.append(new) |
||
171 | |||
172 | return changes |
||
173 |