| Conditions | 15 |
| Total Lines | 54 |
| Code Lines | 40 |
| 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( |
||
| 73 | nlri, |
||
| 74 | Attributes() |
||
| 75 | ) |
||
| 76 | |||
| 77 | while True: |
||
| 78 | command = tokeniser() |
||
| 79 | |||
| 80 | if not command: |
||
| 81 | break |
||
| 82 | |||
| 83 | if command == 'label': |
||
| 84 | nlri.labels = label(tokeniser) |
||
| 85 | continue |
||
| 86 | |||
| 87 | if command == 'rd' or command == 'route-distinguisher': |
||
| 88 | nlri.rd = route_distinguisher(tokeniser) |
||
| 89 | continue |
||
| 90 | |||
| 91 | action = ParseStatic.action.get(command,'') |
||
| 92 | |||
| 93 | if action == 'attribute-add': |
||
| 94 | change.attributes.add(ParseStatic.known[command](tokeniser)) |
||
| 95 | elif action == 'nlri-set': |
||
| 96 | change.nlri.assign(ParseStatic.assign[command],ParseStatic.known[command](tokeniser)) |
||
| 97 | elif action == 'nexthop-and-attribute': |
||
| 98 | nexthop,attribute = ParseStatic.known[command](tokeniser) |
||
| 99 | change.nlri.nexthop = nexthop |
||
| 100 | change.attributes.add(attribute) |
||
| 101 | else: |
||
| 102 | raise ValueError('unknown command "%s"' % command) |
||
| 103 | |||
| 104 | if not check(change,nlri.afi): |
||
| 105 | raise ValueError('invalid route (missing next-hop, label or rd ?)') |
||
| 106 | |||
| 107 | return list(ParseStatic.split(change)) |
||
| 108 | |||
| 182 |