Conditions | 11 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 132 |
Changes | 1 | ||
Bugs | 0 | Features | 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 ActionManager.queue() 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.core.helpers.thread import module |
||
29 | @classmethod |
||
30 | def queue(cls, event, request, session=None, account=None): |
||
31 | if event is None: |
||
32 | return None |
||
33 | |||
34 | obj = None |
||
35 | |||
36 | if request is not None: |
||
37 | request = json.dumps(request) |
||
38 | |||
39 | # Retrieve `account_id` for action |
||
40 | account_id = None |
||
41 | |||
42 | if session: |
||
43 | try: |
||
44 | account_id = session.account_id |
||
45 | except KeyError: |
||
46 | account_id = None |
||
47 | |||
48 | if account_id is None and account: |
||
49 | account_id = account.id |
||
50 | |||
51 | if account_id is None: |
||
52 | log.debug('Unable to find valid account for event %r, session %r', event, session) |
||
53 | return None |
||
54 | |||
55 | if not Preferences.get('scrobble.enabled', account_id): |
||
56 | log.debug('Scrobbler not enabled for account %r', account_id) |
||
57 | return None |
||
58 | |||
59 | # Try queue the event |
||
60 | try: |
||
61 | obj = ActionQueue.create( |
||
62 | account=account_id, |
||
63 | session=session, |
||
64 | |||
65 | progress=session.progress, |
||
66 | |||
67 | part=session.part, |
||
68 | rating_key=session.rating_key, |
||
69 | |||
70 | event=event, |
||
71 | request=request, |
||
72 | |||
73 | queued_at=datetime.utcnow() |
||
74 | ) |
||
75 | log.debug('Queued %r event for %r', event, session) |
||
76 | except (apsw.ConstraintError, peewee.IntegrityError) as ex: |
||
77 | log.info('Event %r has already been queued for session %r: %s', event, session.session_key, ex, exc_info=True) |
||
|
|||
78 | except Exception as ex: |
||
79 | log.warn('Unable to queue event %r for %r: %s', event, session, ex, exc_info=True) |
||
80 | |||
81 | # Ensure process thread is started |
||
82 | cls.start() |
||
83 | |||
84 | return obj |
||
85 | |||
243 |
This check looks for lines that are too long. You can specify the maximum line length.