Conditions | 24 |
Total Lines | 74 |
Code Lines | 50 |
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.bgp.message.open.capability.negotiated.Negotiated._negotiate() 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 |
||
53 | def _negotiate(self): |
||
54 | sent_capa = self.sent_open.capabilities |
||
55 | recv_capa = self.received_open.capabilities |
||
56 | |||
57 | self.holdtime = HoldTime(min(self.sent_open.hold_time, self.received_open.hold_time)) |
||
58 | |||
59 | self.addpath.setup(self.received_open, self.sent_open) |
||
60 | self.asn4 = sent_capa.announced(Capability.CODE.FOUR_BYTES_ASN) and recv_capa.announced( |
||
61 | Capability.CODE.FOUR_BYTES_ASN |
||
62 | ) |
||
63 | self.operational = sent_capa.announced(Capability.CODE.OPERATIONAL) and recv_capa.announced( |
||
64 | Capability.CODE.OPERATIONAL |
||
65 | ) |
||
66 | |||
67 | self.local_as = self.sent_open.asn |
||
68 | self.peer_as = self.received_open.asn |
||
69 | if self.received_open.asn == AS_TRANS and self.asn4: |
||
70 | self.peer_as = recv_capa.get(Capability.CODE.FOUR_BYTES_ASN, self.peer_as) |
||
71 | |||
72 | self.families = [] |
||
73 | if recv_capa.announced(Capability.CODE.MULTIPROTOCOL) and sent_capa.announced(Capability.CODE.MULTIPROTOCOL): |
||
74 | for family in recv_capa[Capability.CODE.MULTIPROTOCOL]: |
||
75 | if family in sent_capa[Capability.CODE.MULTIPROTOCOL]: |
||
76 | self.families.append(family) |
||
77 | |||
78 | self.nexthop = [] |
||
79 | if recv_capa.announced(Capability.CODE.NEXTHOP) and sent_capa.announced(Capability.CODE.NEXTHOP): |
||
80 | for family in recv_capa[Capability.CODE.NEXTHOP]: |
||
81 | if family in sent_capa[Capability.CODE.NEXTHOP]: |
||
82 | self.nexthop.append(family) |
||
83 | |||
84 | if recv_capa.announced(Capability.CODE.ENHANCED_ROUTE_REFRESH) and sent_capa.announced( |
||
85 | Capability.CODE.ENHANCED_ROUTE_REFRESH |
||
86 | ): |
||
87 | self.refresh = REFRESH.ENHANCED # pylint: disable=E1101 |
||
88 | elif recv_capa.announced(Capability.CODE.ROUTE_REFRESH) and sent_capa.announced(Capability.CODE.ROUTE_REFRESH): |
||
89 | self.refresh = REFRESH.NORMAL # pylint: disable=E1101 |
||
90 | |||
91 | if recv_capa.announced(Capability.CODE.EXTENDED_MESSAGE) and sent_capa.announced( |
||
92 | Capability.CODE.EXTENDED_MESSAGE |
||
93 | ): |
||
94 | self.msg_size = ExtendedMessage.EXTENDED_SIZE |
||
95 | |||
96 | self.multisession = sent_capa.announced(Capability.CODE.MULTISESSION) and recv_capa.announced( |
||
97 | Capability.CODE.MULTISESSION |
||
98 | ) |
||
99 | self.multisession |= sent_capa.announced(Capability.CODE.MULTISESSION_CISCO) and recv_capa.announced( |
||
100 | Capability.CODE.MULTISESSION_CISCO |
||
101 | ) |
||
102 | |||
103 | if self.multisession: |
||
104 | sent_ms_capa = set(sent_capa[Capability.CODE.MULTISESSION]) |
||
105 | recv_ms_capa = set(recv_capa[Capability.CODE.MULTISESSION]) |
||
106 | |||
107 | if sent_ms_capa == set([]): |
||
108 | sent_ms_capa = set([Capability.CODE.MULTIPROTOCOL]) |
||
109 | if recv_ms_capa == set([]): |
||
110 | recv_ms_capa = set([Capability.CODE.MULTIPROTOCOL]) |
||
111 | |||
112 | if sent_ms_capa != recv_ms_capa: |
||
113 | self.multisession = (2, 8, 'multisession, our peer did not reply with the same sessionid') |
||
114 | |||
115 | # The way we implement MS-BGP, we only send one MP per session |
||
116 | # therefore we can not collide due to the way we generate the configuration |
||
117 | |||
118 | for capa in sent_ms_capa: |
||
119 | # no need to check that the capability exists, we generated it |
||
120 | # checked it is what we sent and only send MULTIPROTOCOL |
||
121 | if sent_capa[capa] != recv_capa[capa]: |
||
122 | self.multisession = (2, 8, 'when checking session id, capability %s did not match' % str(capa)) |
||
123 | break |
||
124 | |||
125 | elif sent_capa.announced(Capability.CODE.MULTISESSION): |
||
126 | self.multisession = (2, 9, 'multisession is mandatory with this peer') |
||
127 | |||
224 |