Conditions | 11 |
Total Lines | 52 |
Code Lines | 44 |
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 bika.lims.browser.worksheet.ajax.AttachAnalyses.__call__() 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 -*- |
||
43 | if not mtool.getMemberById(value): |
||
44 | return |
||
45 | self.context.setAnalyst(value) |
||
46 | |||
47 | |||
48 | class SetInstrument(BrowserView): |
||
49 | """The Instrument dropdown sets worksheet.Instrument immediately |
||
50 | """ |
||
51 | |||
52 | def __init__(self, context, request): |
||
53 | self.context = context |
||
54 | self.request = request |
||
55 | |||
56 | def __call__(self): |
||
57 | rc = getToolByName(self.context, REFERENCE_CATALOG) |
||
58 | plone.protect.CheckAuthenticator(self.request) |
||
59 | plone.protect.PostOnly(self.request) |
||
60 | value = self.request.get('value', '') |
||
61 | if not value: |
||
62 | raise Exception("Invalid instrument") |
||
63 | instrument = rc.lookupObject(value) |
||
64 | if not instrument: |
||
65 | raise Exception("Unable to lookup instrument") |
||
66 | self.context.setInstrument(instrument) |
||
67 |