Conditions | 21 |
Total Lines | 63 |
Code Lines | 53 |
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.reactor.api.command.neighbor.show_neighbor() 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 |
||
129 | @Command.register('text', 'show neighbor', False, ['summary', 'extensive', 'configuration']) |
||
130 | def show_neighbor(self, reactor, service, command): |
||
131 | words = command.split() |
||
132 | |||
133 | extensive = 'extensive' in words |
||
134 | configuration = 'configuration' in words |
||
135 | summary = 'summary' in words |
||
136 | |||
137 | if summary: |
||
138 | words.remove('summary') |
||
139 | if extensive: |
||
140 | words.remove('extensive') |
||
141 | if configuration: |
||
142 | words.remove('configuration') |
||
143 | |||
144 | limit = words[-1] if words[-1] != 'neighbor' else '' |
||
145 | |||
146 | def callback_configuration(): |
||
147 | for neighbor_name in reactor.configuration.neighbors.keys(): |
||
148 | neighbor = reactor.configuration.neighbors.get(neighbor_name, None) |
||
149 | if not neighbor: |
||
150 | continue |
||
151 | if limit and limit not in neighbor_name: |
||
152 | continue |
||
153 | for line in str(neighbor).split('\n'): |
||
154 | reactor.processes.write(service, line) |
||
155 | yield True |
||
156 | reactor.processes.answer_done(service) |
||
157 | |||
158 | def callback_extensive(): |
||
159 | for peer_name in reactor.peers(): |
||
160 | if limit and limit not in reactor.neighbor_name(peer_name): |
||
161 | continue |
||
162 | for line in Neighbor.extensive(reactor.neighbor_cli_data(peer_name)).split('\n'): |
||
163 | reactor.processes.write(service, line) |
||
164 | yield True |
||
165 | reactor.processes.answer_done(service) |
||
166 | |||
167 | def callback_summary(): |
||
168 | reactor.processes.write(service, Neighbor.summary_header) |
||
169 | for peer_name in reactor.established_peers(): |
||
170 | if limit and limit != reactor.neighbor_ip(peer_name): |
||
171 | continue |
||
172 | for line in Neighbor.summary(reactor.neighbor_cli_data(peer_name)).split('\n'): |
||
173 | reactor.processes.write(service, line) |
||
174 | yield True |
||
175 | reactor.processes.answer_done(service) |
||
176 | |||
177 | if summary: |
||
178 | reactor.asynchronous.schedule(service, command, callback_summary()) |
||
179 | return True |
||
180 | |||
181 | if extensive: |
||
182 | reactor.asynchronous.schedule(service, command, callback_extensive()) |
||
183 | return True |
||
184 | |||
185 | if configuration: |
||
186 | reactor.asynchronous.schedule(service, command, callback_configuration()) |
||
187 | return True |
||
188 | |||
189 | reactor.processes.write(service, 'please specify summary, extensive or configuration') |
||
190 | reactor.processes.write(service, 'you can filter by peer ip address adding it after the word neighbor') |
||
191 | reactor.processes.answer_done(service) |
||
192 |