Conditions | 27 |
Total Lines | 61 |
Code Lines | 45 |
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 ActionHandler.startPoll 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 | import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1'; |
||
49 | |||
50 | /** |
||
51 | * Handle the custom start_poll action. |
||
52 | * |
||
53 | * @returns {object} Response to send back to Chat |
||
54 | */ |
||
55 | async startPoll() { |
||
56 | // Get the form values |
||
57 | const formValues = this.event.common?.formInputs; |
||
58 | const topic = formValues?.['topic']?.stringInputs?.value?.[0]?.trim() ?? ''; |
||
59 | const isAnonymous = formValues?.['is_anonymous']?.stringInputs?.value?.[0] === '1'; |
||
60 | const allowAddOption = formValues?.['allow_add_option']?.stringInputs?.value?.[0] === '1'; |
||
61 | const choices = []; |
||
62 | const votes: Votes = {}; |
||
63 | |||
64 | for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) { |
||
65 | const choice = formValues?.[`option${i}`]?.stringInputs?.value?.[0]?.trim(); |
||
66 | if (choice) { |
||
67 | choices.push(choice); |
||
68 | votes[i] = []; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | if (!topic || choices.length === 0) { |
||
73 | // Incomplete form submitted, rerender |
||
74 | const dialog = new NewPollFormCard({ |
||
75 | topic, |
||
76 | choices, |
||
77 | }).create(); |
||
78 | return { |
||
79 | actionResponse: { |
||
80 | type: 'DIALOG', |
||
81 | dialogAction: { |
||
82 | dialog: { |
||
83 | body: dialog, |
||
84 | }, |
||
85 | }, |
||
86 | }, |
||
87 | }; |
||
88 | } |
||
89 | const pollCard = new PollCard({ |
||
90 | topic: topic, choiceCreator: undefined, |
||
91 | author: this.event.user, |
||
92 | choices: choices, |
||
93 | votes: votes, |
||
94 | anon: isAnonymous, |
||
95 | optionable: allowAddOption, |
||
96 | }).createCardWithId(); |
||
97 | // Valid configuration, make the voting card to display in the space |
||
98 | const message = { |
||
99 | cardsV2: [pollCard], |
||
100 | }; |
||
101 | const request = { |
||
102 | parent: this.event.space?.name, |
||
103 | requestBody: message, |
||
104 | }; |
||
105 | const apiResponse = await callMessageApi('create', request); |
||
106 | if (apiResponse) { |
||
107 | return createStatusActionResponse('Poll started.', 'OK'); |
||
108 | } else { |
||
109 | return createStatusActionResponse('Failed to start poll.', 'UNKNOWN'); |
||
110 | } |
||
193 |