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