Conditions | 16 |
Total Lines | 106 |
Code Lines | 77 |
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 isomer.schemata.base.base_object() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | #!/usr/bin/env python |
||
97 | def base_object( |
||
98 | name, |
||
99 | no_additional=False, |
||
100 | no_perms=False, |
||
101 | no_color=False, |
||
102 | has_owner=True, |
||
103 | hide_owner=True, |
||
104 | has_uuid=True, |
||
105 | roles_write=None, |
||
106 | roles_read=None, |
||
107 | roles_list=None, |
||
108 | roles_create=None, |
||
109 | all_roles=None, |
||
110 | ): |
||
111 | """Generates a basic object with RBAC properties""" |
||
112 | base_schema = {"id": "#" + name, "type": "object", "name": name, "properties": {}} |
||
113 | |||
114 | if no_additional: |
||
115 | base_schema['additionalProperties'] = False |
||
116 | |||
117 | if not no_perms: |
||
118 | if all_roles: |
||
119 | roles_create = ["admin", all_roles] |
||
120 | roles_write = ["admin", all_roles] |
||
121 | roles_read = ["admin", all_roles] |
||
122 | roles_list = ["admin", all_roles] |
||
123 | else: |
||
124 | if roles_write is None: |
||
125 | roles_write = ["admin"] |
||
126 | if roles_read is None: |
||
127 | roles_read = ["admin"] |
||
128 | if roles_list is None: |
||
129 | roles_list = ["admin"] |
||
130 | if roles_create is None: |
||
131 | roles_create = ["admin"] |
||
132 | |||
133 | if isinstance(roles_create, str): |
||
134 | roles_create = [roles_create] |
||
135 | if isinstance(roles_write, str): |
||
136 | roles_write = [roles_write] |
||
137 | if isinstance(roles_read, str): |
||
138 | roles_read = [roles_read] |
||
139 | if isinstance(roles_list, str): |
||
140 | roles_list = [roles_list] |
||
141 | |||
142 | if has_owner: |
||
143 | roles_write.append("owner") |
||
144 | roles_read.append("owner") |
||
145 | roles_list.append("owner") |
||
146 | |||
147 | base_schema["roles_create"] = roles_create |
||
148 | base_schema["properties"].update( |
||
149 | { |
||
150 | "perms": { |
||
151 | "id": "#perms", |
||
152 | "type": "object", |
||
153 | "name": "perms", |
||
154 | "properties": { |
||
155 | "write": { |
||
156 | "type": "array", |
||
157 | "default": roles_write, |
||
158 | "items": {"type": "string"}, |
||
159 | }, |
||
160 | "read": { |
||
161 | "type": "array", |
||
162 | "default": roles_read, |
||
163 | "items": {"type": "string"}, |
||
164 | }, |
||
165 | "list": { |
||
166 | "type": "array", |
||
167 | "default": roles_list, |
||
168 | "items": {"type": "string"}, |
||
169 | }, |
||
170 | }, |
||
171 | "default": {}, |
||
172 | "x-schema-form": {"condition": "false"}, |
||
173 | }, |
||
174 | "name": {"type": "string", "description": "Name of " + name}, |
||
175 | } |
||
176 | ) |
||
177 | |||
178 | if has_owner: |
||
179 | # TODO: Schema should allow specification of non-local owners as |
||
180 | # well as special accounts like admin or even system perhaps |
||
181 | # base_schema['required'] = base_schema.get('required', []) |
||
182 | # base_schema['required'].append('owner') |
||
183 | base_schema["properties"].update( |
||
184 | {"owner": uuid_object(title="Unique Owner ID", display=hide_owner)} |
||
185 | ) |
||
186 | else: |
||
187 | base_schema["no_perms"] = True |
||
188 | |||
189 | if not no_color: |
||
190 | base_schema["properties"].update( |
||
191 | {"color": {"type": "string", "format": "colorpicker"}} |
||
192 | ) |
||
193 | |||
194 | # TODO: Using this causes all sorts of (obvious) problems with the object |
||
195 | # manager |
||
196 | if has_uuid: |
||
197 | base_schema["properties"].update( |
||
198 | {"uuid": uuid_object(title="Unique " + name + " ID", display=False)} |
||
199 | ) |
||
200 | base_schema["required"] = ["uuid"] |
||
201 | |||
202 | return base_schema |
||
203 | |||
227 |