Conditions | 14 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
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 CreateAlertAction.run() 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 |
||
19 | def run(self, message, teams=None, alias=None, |
||
20 | description=None, recipients=None, actions=None, |
||
21 | source="StackStorm", tags=None, details=None, |
||
22 | entity=None, user=None, note=None): |
||
23 | """ |
||
24 | """ |
||
25 | |||
26 | if len(message) > 130: |
||
27 | raise ValueError("Message length ({}) is over 130 chars".format( |
||
28 | len(message))) |
||
29 | |||
30 | body = {"apiKey": self.api_key, |
||
31 | "message": message} |
||
32 | |||
33 | if teams: |
||
34 | body["teams"] = teams |
||
35 | |||
36 | if alias: |
||
37 | body["alias"] = alias |
||
38 | |||
39 | if description: |
||
40 | if len(description) > 15000: |
||
41 | raise ValueError("Description is too long, can't be over 15000 chars") |
||
42 | else: |
||
43 | body["description"] = description |
||
44 | |||
45 | if recipients: |
||
46 | body["recipients"] = recipients |
||
47 | |||
48 | if actions: |
||
49 | body["actions"] = actions |
||
50 | |||
51 | if source: |
||
52 | body["source"] = source |
||
53 | |||
54 | if tags: |
||
55 | body["tags"] = tags |
||
56 | |||
57 | if details: |
||
58 | body["details"] = details |
||
59 | |||
60 | if entity: |
||
61 | body["entity"] = entity |
||
62 | |||
63 | if user: |
||
64 | body['user'] = user |
||
65 | |||
66 | if note: |
||
67 | body['note'] = note |
||
68 | |||
69 | data = self._req("POST", |
||
70 | "v1/json/alert", |
||
71 | body=body) |
||
72 | |||
73 | return data |
||
74 |