Conditions | 16 |
Total Lines | 53 |
Code Lines | 43 |
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 build.bika.lims.adapters.identifiers.SearchableText() 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 -*- |
||
87 | class IHaveIdentifiersSchemaExtender(object): |
||
88 | adapts(IHaveIdentifiers) |
||
89 | implements(IOrderableSchemaExtender) |
||
90 | |||
91 | fields = [ |
||
92 | Identifiers, |
||
93 | ] |
||
94 | |||
95 | def __init__(self, context): |
||
96 | self.context = context |
||
97 | |||
98 | def getOrder(self, schematas): |
||
99 | """Return modified order of field schemats. |
||
100 | """ |
||
101 | schemata = self.context.schema['description'].schemata |
||
102 | fields = schematas[schemata] |
||
103 | fields.insert(fields.index('description') + 1, |
||
104 | 'Identifiers') |
||
105 | return schematas |
||
106 | |||
107 | def getFields(self): |
||
108 | return self.fields |
||
109 | |||
110 | |||
111 | class IHaveIdentifiersSchemaModifier(object): |
||
112 | adapts(IHaveIdentifiers) |
||
113 | implements(ISchemaModifier) |
||
114 | |||
115 | def __init__(self, context): |
||
116 | self.context = context |
||
117 | |||
118 | def fiddle(self, schema): |
||
119 | schemata = schema['description'].schemata |
||
120 | schema['Identifiers'].schemata = schemata |
||
121 | schema.moveField('Identifiers', after='description') |
||
122 | return schema |
||
123 | |||
124 | |||
125 | class ajaxGetIdentifierTypes(BrowserView): |
||
126 | """ Identifier types vocabulary source for jquery combo dropdown box |
||
127 | """ |
||
128 | |||
129 | def __call__(self): |
||
130 | plone.protect.CheckAuthenticator(self.request) |
||
131 | searchTerm = 'searchTerm' in self.request and self.request[ |
||
132 | 'searchTerm'].lower() or '' |
||
133 | page = self.request['page'] |
||
134 | nr_rows = self.request['rows'] |
||
135 | sord = self.request['sord'] |
||
136 | sidx = self.request['sidx'] |
||
137 | rows = [] |
||
138 | |||
139 | # lookup objects from ZODB |
||
140 | brains = self.bika_setup_catalog(portal_type='IdentifierType', |
||
162 |