| Conditions | 10 |
| Total Lines | 51 |
| Lines | 51 |
| Ratio | 100 % |
| Tests | 0 |
| CRAP Score | 110 |
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 ClientRuleService.update() 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 | from plugin.api.core.base import Service, expose |
||
| 31 | @expose |
||
| 32 | def update(self, current, full=False): |
||
| 33 | result = [] |
||
| 34 | |||
| 35 | # Build array of current ids |
||
| 36 | current_ids = [ |
||
| 37 | r.get('id') |
||
| 38 | for r in current |
||
| 39 | if r.get('id') is not None |
||
| 40 | ] |
||
| 41 | |||
| 42 | # Delete rules |
||
| 43 | deleted_rules = [ |
||
| 44 | rule |
||
| 45 | for rule in ClientRuleManager.get.all() |
||
| 46 | if rule.id not in current_ids |
||
| 47 | ] |
||
| 48 | |||
| 49 | for rule in deleted_rules: |
||
| 50 | rule.delete_instance() |
||
| 51 | |||
| 52 | log.debug('Deleted %r', rule) |
||
| 53 | |||
| 54 | # Create/Update client rules |
||
| 55 | for r in current: |
||
| 56 | id = r.pop('id', None) |
||
| 57 | |||
| 58 | if id is None: |
||
| 59 | # Create new rule |
||
| 60 | rule = ClientRuleManager.create(**r) |
||
| 61 | |||
| 62 | log.debug('Created %r', rule) |
||
| 63 | result.append(rule) |
||
| 64 | continue |
||
| 65 | |||
| 66 | # Retrieve existing rule |
||
| 67 | rule = ClientRuleManager.get(id=id) |
||
| 68 | |||
| 69 | # Update rule |
||
| 70 | ClientRuleManager.update(rule, r) |
||
| 71 | |||
| 72 | log.debug('Updated %r', rule) |
||
| 73 | result.append(rule) |
||
| 74 | |||
| 75 | # Ensure result is sorted by priority |
||
| 76 | result = sorted(result, key=lambda item: item.priority) |
||
| 77 | |||
| 78 | # Convert rules to serializable dictionaries |
||
| 79 | return [ |
||
| 80 | r.to_json(full=full) |
||
| 81 | for r in result |
||
| 82 | ] |
||
| 83 |
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example
could be written as