Conditions | 10 |
Total Lines | 73 |
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 senaite.core.browser.widgets.services_widget.ServicesWidget.folderitem() 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 -*- |
||
200 | def folderitem(self, obj, item, index): |
||
201 | """Service triggered each time an item is iterated in folderitems. |
||
202 | |||
203 | The use of this service prevents the extra-loops in child objects. |
||
204 | |||
205 | :obj: the instance of the class to be foldered |
||
206 | :item: dict containing the properties of the object to be used by |
||
207 | the template |
||
208 | :index: current index of the item |
||
209 | """ |
||
210 | item = super(ServicesWidget, self).folderitem(obj, item, index) |
||
211 | |||
212 | # ensure we have an object and not a brain |
||
213 | obj = api.get_object(obj) |
||
214 | uid = api.get_uid(obj) |
||
215 | url = api.get_url(obj) |
||
216 | title = api.get_title(obj) |
||
217 | keyword = obj.getKeyword() |
||
218 | |||
219 | # get the category |
||
220 | if self.show_categories_enabled(): |
||
221 | category = obj.getCategoryTitle() |
||
222 | if category not in self.categories: |
||
223 | self.categories.append(category) |
||
224 | item["category"] = category |
||
225 | |||
226 | hidden = False |
||
227 | # get the hidden setting from the records |
||
228 | if self.records.get(uid): |
||
229 | record = self.records.get(uid, {}) or {} |
||
230 | hidden = record.get("hidden", False) |
||
231 | else: |
||
232 | # get the default value from the service |
||
233 | hidden = obj.getHidden() |
||
234 | |||
235 | item["replace"]["Title"] = get_link(url, value=title) |
||
236 | item["Price"] = self.format_price(obj.Price) |
||
237 | item["allow_edit"] = self.get_editable_columns() |
||
238 | item["selected"] = False |
||
239 | item["Hidden"] = hidden |
||
240 | item["replace"]["Hidden"] = _("Yes") if hidden else _("No") |
||
241 | item["selected"] = uid in self.records |
||
242 | item["Keyword"] = keyword |
||
243 | item["replace"]["Keyword"] = "<code>{}</code>".format(keyword) |
||
244 | |||
245 | # Add methods |
||
246 | methods = obj.getMethods() |
||
247 | if methods: |
||
248 | links = map( |
||
249 | lambda m: get_link( |
||
250 | m.absolute_url(), value=m.Title(), css_class="link"), |
||
251 | methods) |
||
252 | item["replace"]["Methods"] = ", ".join(links) |
||
253 | else: |
||
254 | item["methods"] = "" |
||
255 | |||
256 | # Unit |
||
257 | unit = obj.getUnit() |
||
258 | item["Unit"] = unit or "" |
||
259 | item["replace"]["Unit"] = unit and format_supsub(unit) or "" |
||
260 | |||
261 | # Icons |
||
262 | after_icons = "" |
||
263 | if obj.getAccredited(): |
||
264 | after_icons += get_image( |
||
265 | "accredited.png", title=_("Accredited")) |
||
266 | if obj.getAttachmentRequired(): |
||
267 | after_icons += get_image( |
||
268 | "attach_reqd.png", title=_("Attachment required")) |
||
269 | if after_icons: |
||
270 | item["after"]["Title"] = after_icons |
||
271 | |||
272 | return item |
||
273 |