| Conditions | 20 |
| Total Lines | 53 |
| Code Lines | 47 |
| 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.mpls.prefix_sid() 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 |
||
| 66 | def prefix_sid (tokeniser): |
||
| 67 | sr_attrs = [] |
||
| 68 | srgbs = [] |
||
| 69 | srgb_data = [] |
||
| 70 | value = tokeniser() |
||
| 71 | get_range = False |
||
| 72 | consume_extra = False |
||
| 73 | try: |
||
| 74 | if value == '[': |
||
| 75 | label_sid = tokeniser() |
||
| 76 | while True: |
||
| 77 | value = tokeniser() |
||
| 78 | if value == '[': |
||
| 79 | consume_extra = True |
||
| 80 | continue |
||
| 81 | if value == ',': |
||
| 82 | continue |
||
| 83 | if value == '(': |
||
| 84 | while True: |
||
| 85 | value = tokeniser() |
||
| 86 | if value == ')': |
||
| 87 | break |
||
| 88 | if value == ',': |
||
| 89 | get_range = True |
||
| 90 | continue |
||
| 91 | if get_range: |
||
| 92 | srange = value |
||
| 93 | get_range = False |
||
| 94 | else: |
||
| 95 | base = value |
||
| 96 | if value == ')': |
||
| 97 | srgb_data.append((base,srange)) |
||
| 98 | continue |
||
| 99 | if value == ']': |
||
| 100 | break |
||
| 101 | if consume_extra: |
||
| 102 | tokeniser() |
||
| 103 | except Exception as e: |
||
| 104 | raise ValueError('could not parse BGP PrefixSid attribute: {}'.format(e)) |
||
| 105 | |||
| 106 | if int(label_sid) < pow(2,32): |
||
| 107 | sr_attrs.append(SrLabelIndex(int(label_sid))) |
||
| 108 | |||
| 109 | for srgb in srgb_data: |
||
| 110 | if (len(srgb) == 2 and int(srgb[0]) < pow(2,24) and int(srgb[1]) < pow(2,24)): |
||
| 111 | srgbs.append((int(srgb[0]),int(srgb[1]))) |
||
| 112 | else: |
||
| 113 | raise ValueError('could not parse SRGB tupple') |
||
| 114 | |||
| 115 | if srgbs: |
||
| 116 | sr_attrs.append(SrGb(srgbs)) |
||
| 117 | |||
| 118 | return PrefixSid(sr_attrs) |
||
| 119 | |||
| 151 |