Conditions | 26 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
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.preparer() 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 -*- |
||
108 | def preparer(self, adres): |
||
109 | if adres is None or not adres: |
||
110 | return null # pragma: no cover |
||
111 | request = self.bindings['request'] |
||
112 | crab_gateway = request.crab_gateway() |
||
113 | if 'land' in adres and adres.get('land').upper() == 'BE': |
||
114 | if adres.get('gemeente_id', None) is None: |
||
115 | if adres.get('gemeente', None) is None: |
||
116 | return None |
||
117 | gemeente = adres.get('gemeente') |
||
118 | gewest_ids = [2, 1, 3] |
||
119 | for gewest_id in gewest_ids: |
||
120 | gemeenten = crab_gateway.list_gemeenten(gewest_id) |
||
121 | gemeente_val = next((g for g in gemeenten if g.naam.lower() == gemeente.lower()), None) |
||
122 | if gemeente_val: |
||
123 | adres['gemeente'] = "" + gemeente_val.naam |
||
124 | adres['gemeente_id'] = gemeente_val.id |
||
125 | break |
||
126 | if adres.get('gemeente_id', None) is None: |
||
127 | return adres |
||
128 | gemeente_id = adres.get('gemeente_id') |
||
129 | straat_id = adres.get('straat_id', None) |
||
130 | straat = adres.get('straat', None) |
||
131 | huisnummer_id = adres.get('huisnummer_id', None) |
||
132 | huisnummer = adres.get('huisnummer', None) |
||
133 | subadres_id = adres.get('subadres_id', None) |
||
134 | subadres = adres.get('subadres', None) |
||
135 | try: |
||
136 | gemeente = crab_gateway.get_gemeente_by_id(gemeente_id) |
||
137 | except (GatewayRuntimeException, AttributeError): |
||
138 | adres['gemeente'] = None |
||
139 | return adres |
||
140 | if gemeente: |
||
141 | adres['gemeente'] = "" + gemeente.naam |
||
142 | if straat_id: |
||
143 | straat_val = next((s for s in gemeente.straten if s.id == straat_id), None) |
||
144 | if straat_val: |
||
145 | adres['straat'] = "" + straat_val.label |
||
146 | num_val = process_huisnummer(adres, huisnummer_id, huisnummer, straat_val) |
||
147 | if num_val: |
||
148 | process_subadres(adres, subadres_id, subadres, num_val) |
||
149 | if not straat_id and straat: |
||
150 | straat_val = next((s for s in gemeente.straten if s.label.lower() == straat.lower()), None) |
||
151 | if straat_val: |
||
152 | adres['straat_id'] = straat_val.id |
||
153 | num_val = process_huisnummer(adres, huisnummer_id, huisnummer, straat_val) |
||
154 | if num_val: |
||
155 | process_subadres(adres, subadres_id, subadres, num_val) |
||
156 | else: |
||
157 | adres['gemeente_id'] = None |
||
158 | adres['straat_id'] = None |
||
159 | adres['huisnummer_id'] = None |
||
160 | adres['subadres_id'] = None |
||
161 | if 'land' in adres: |
||
162 | adres['land'] = adres.get('land').upper() |
||
163 | return adres |
||
164 | |||
263 |