Conditions | 24 |
Total Lines | 96 |
Lines | 50 |
Ratio | 52.08 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 CrabAdresSchemaNode.validator() 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 | # -*- coding: utf-8 -*- |
||
165 | def validator(self, node, adres): |
||
166 | if not adres: |
||
167 | return |
||
168 | request = self.bindings['request'] |
||
169 | crab_gateway = request.crab_gateway() |
||
170 | if 'land' in adres: |
||
171 | land = adres.get('land') |
||
172 | try: |
||
173 | pycountry.countries.get(alpha2=land) |
||
174 | except KeyError: |
||
175 | raise colander.Invalid( |
||
176 | node, |
||
177 | 'ongeldige landcode %s, dit is geen ISO 3166 code' % |
||
178 | land |
||
179 | ) |
||
180 | if land == 'BE': |
||
181 | gemeente = adres.get('gemeente', None) |
||
182 | gemeente_id = adres.get('gemeente_id', None) |
||
183 | straat_id = adres.get('straat_id', None) |
||
184 | huisnummer_id = adres.get('huisnummer_id', None) |
||
185 | postcode = adres.get('postcode', None) |
||
186 | subadres_id = adres.get('subadres_id', None) |
||
187 | if gemeente_id is None: |
||
188 | raise colander.Invalid( |
||
189 | node, |
||
190 | 'geen correcte gemeente_id gevonden voor de gemeente {0}'.format(gemeente) |
||
191 | ) |
||
192 | if gemeente is None: |
||
193 | raise colander.Invalid( |
||
194 | node, |
||
195 | 'ongeldig gemeente_id {0}'.format(gemeente_id) |
||
196 | ) |
||
197 | View Code Duplication | if straat_id is not None: |
|
|
|||
198 | gemeente = crab_gateway.get_gemeente_by_id(gemeente_id) |
||
199 | try: |
||
200 | straat = crab_gateway.get_straat_by_id(straat_id) |
||
201 | except (GatewayRuntimeException, AttributeError): |
||
202 | raise colander.Invalid( |
||
203 | node, |
||
204 | 'ongeldig straat_id' |
||
205 | ) |
||
206 | if straat.gemeente_id != gemeente_id: |
||
207 | raise colander.Invalid( |
||
208 | node, |
||
209 | 'de straat %s met id %s ligt niet in gemeente %s' % |
||
210 | (adres.get('straat', ''), straat_id, gemeente.naam) |
||
211 | ) |
||
212 | if huisnummer_id is not None: |
||
213 | try: |
||
214 | huisnummer = crab_gateway.get_huisnummer_by_id(huisnummer_id) |
||
215 | except (GatewayRuntimeException, AttributeError): |
||
216 | raise colander.Invalid( |
||
217 | node, |
||
218 | 'ongeldig huisnummer_id' |
||
219 | ) |
||
220 | if huisnummer.straat_id != straat_id: |
||
221 | raise colander.Invalid( |
||
222 | node, |
||
223 | 'het huisnummer %s met id %s ligt niet in straat %s' % |
||
224 | (adres.get('huisnummer', ''), huisnummer_id, straat.label) |
||
225 | ) |
||
226 | if postcode is not None: |
||
227 | postkanton = crab_gateway.get_postkanton_by_huisnummer(huisnummer_id) |
||
228 | if postcode != str(postkanton.id): |
||
229 | raise colander.Invalid( |
||
230 | node, |
||
231 | 'postcode %s is niet correct voor dit adres, mogelijke postcode is %s' % |
||
232 | (postcode, postkanton.id) |
||
233 | ) |
||
234 | if subadres_id is not None: |
||
235 | try: |
||
236 | subadres = crab_gateway.get_subadres_by_id(subadres_id) |
||
237 | except (GatewayRuntimeException, AttributeError): |
||
238 | raise colander.Invalid( |
||
239 | node, |
||
240 | 'ongeldig subadres_id' |
||
241 | ) |
||
242 | if subadres.huisnummer_id != huisnummer_id: |
||
243 | raise colander.Invalid( |
||
244 | node, |
||
245 | 'het subadres %s met id %s ligt niet op huisnummer %s' % |
||
246 | (adres.get('subadres', ''), subadres_id, huisnummer.huisnummer) |
||
247 | ) |
||
248 | if straat_id is None and huisnummer_id is not None: |
||
249 | raise colander.Invalid( |
||
250 | node, |
||
251 | 'als er een huisnummer_id wordt gegeven, moet men ook het straat_id invullen' |
||
252 | ) |
||
253 | if huisnummer_id is None and postcode is not None: |
||
254 | postkantons = crab_gateway.list_postkantons_by_gemeente(gemeente_id) |
||
255 | postkantons = [str(pk.id) for pk in postkantons] |
||
256 | if postcode not in postkantons: |
||
257 | raise colander.Invalid( |
||
258 | node, |
||
259 | 'postcode %s is niet correct voor dit adres, mogelijke postcode(s) zijn %s' % |
||
260 | (postcode, postkantons) |
||
261 | ) |
||
263 |