Conditions | 11 |
Total Lines | 54 |
Lines | 0 |
Ratio | 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 st2common.cmd.purge_executions() 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
73 | |||
74 | if not timestamp: |
||
75 | LOG.error('Please supply a timestamp for purging models. Aborting.') |
||
76 | return 1 |
||
77 | else: |
||
78 | timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ') |
||
79 | timestamp = timestamp.replace(tzinfo=pytz.UTC) |
||
80 | |||
81 | try: |
||
82 | purge_executions(logger=LOG, timestamp=timestamp, action_ref=action_ref, |
||
83 | purge_incomplete=purge_incomplete) |
||
84 | except Exception as e: |
||
85 | LOG.exception(str(e)) |
||
86 | return FAILURE_EXIT_CODE |
||
87 | finally: |
||
88 | common_teardown() |
||
89 | |||
90 | return SUCCESS_EXIT_CODE |
||
91 |