Conditions | 15 |
Total Lines | 51 |
Code Lines | 38 |
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.route() 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 |
||
54 | @ParseStatic.register('route', 'append-route') |
||
55 | def route(tokeniser): |
||
56 | action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW |
||
57 | ipmask = prefix(tokeniser) |
||
58 | check = lambda change, afi: True |
||
59 | |||
60 | if 'rd' in tokeniser.tokens or 'route-distinguisher' in tokeniser.tokens: |
||
61 | nlri = IPVPN(IP.toafi(ipmask.top()), SAFI.mpls_vpn, action) |
||
62 | check = AnnounceVPN.check |
||
63 | elif 'label' in tokeniser.tokens: |
||
64 | nlri = Label(IP.toafi(ipmask.top()), SAFI.nlri_mpls, action) |
||
65 | check = AnnounceLabel.check |
||
66 | else: |
||
67 | nlri = INET(IP.toafi(ipmask.top()), IP.tosafi(ipmask.top()), action) |
||
68 | check = AnnouncePath.check |
||
69 | |||
70 | nlri.cidr = CIDR(ipmask.pack(), ipmask.mask) |
||
71 | |||
72 | change = Change(nlri, Attributes()) |
||
73 | |||
74 | while True: |
||
75 | command = tokeniser() |
||
76 | |||
77 | if not command: |
||
78 | break |
||
79 | |||
80 | if command == 'label': |
||
81 | nlri.labels = label(tokeniser) |
||
82 | continue |
||
83 | |||
84 | if command == 'rd' or command == 'route-distinguisher': |
||
85 | nlri.rd = route_distinguisher(tokeniser) |
||
86 | continue |
||
87 | |||
88 | action = ParseStatic.action.get(command, '') |
||
89 | |||
90 | if action == 'attribute-add': |
||
91 | change.attributes.add(ParseStatic.known[command](tokeniser)) |
||
92 | elif action == 'nlri-set': |
||
93 | change.nlri.assign(ParseStatic.assign[command], ParseStatic.known[command](tokeniser)) |
||
94 | elif action == 'nexthop-and-attribute': |
||
95 | nexthop, attribute = ParseStatic.known[command](tokeniser) |
||
96 | change.nlri.nexthop = nexthop |
||
97 | change.attributes.add(attribute) |
||
98 | else: |
||
99 | raise ValueError('unknown command "%s"' % command) |
||
100 | |||
101 | if not check(change, nlri.afi): |
||
102 | raise ValueError('invalid route (missing next-hop, label or rd ?)') |
||
103 | |||
104 | return list(ParseStatic.split(change)) |
||
105 | |||
173 |