Conditions | 14 |
Total Lines | 66 |
Code Lines | 42 |
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 vote.ts ➔ saveVotes 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/chat'; |
||
3 | |||
4 | /** |
||
5 | * Creates a small progress bar to show percent of votes for an option. Since |
||
6 | * width is limited, the percentage is scaled to 20 steps (5% increments). |
||
7 | * |
||
8 | * @param {number} choice - The choice index |
||
9 | * @param {object} voter - The voter |
||
10 | * @param {PollState} state - PollState |
||
11 | * @returns {Votes} Map of cast votes keyed by choice index |
||
12 | */ |
||
13 | export function saveVotes(choice: number, voter: Voter, state: PollState) { |
||
14 | const votes: Votes = state.votes!; |
||
15 | const isAnonymous = state.anon || false; |
||
16 | const maxVotes = state.voteLimit === 0 ? state.choices.length : state.voteLimit || 1; |
||
17 | if (maxVotes === 1) { |
||
18 | Object.keys(votes).forEach(function(choiceIndex) { |
||
19 | if (votes[choiceIndex]) { |
||
20 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
21 | if (existed > -1) { |
||
22 | votes[choiceIndex].splice(existed, 1); |
||
23 | } |
||
24 | } |
||
25 | }); |
||
26 | } else { |
||
27 | // get current voter total vote |
||
28 | let voteCount = 0; |
||
29 | let voted = false; |
||
30 | Object.keys(votes).forEach(function(choiceIndex) { |
||
31 | if (votes[choiceIndex]) { |
||
32 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
33 | if (existed > -1) { |
||
34 | voteCount += 1; |
||
35 | } |
||
36 | if (existed > -1 && parseInt(choiceIndex) === choice) { |
||
37 | voted = true; |
||
38 | } |
||
39 | } |
||
40 | }); |
||
41 | if (voteCount >= maxVotes || voted) { |
||
42 | let deleted = false; |
||
43 | Object.keys(votes).forEach(function(choiceIndex) { |
||
44 | if (votes[choiceIndex]) { |
||
45 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
46 | if (((voteCount >= maxVotes && existed > -1 && !voted) || |
||
47 | (voted && parseInt(choiceIndex) === choice)) && !deleted) { |
||
48 | votes[choiceIndex].splice(existed, 1); |
||
49 | deleted = true; |
||
50 | } |
||
51 | } |
||
52 | }); |
||
53 | } |
||
54 | if (voted) { |
||
55 | return votes; |
||
56 | } |
||
57 | } |
||
58 | if (isAnonymous) { |
||
59 | delete voter.name; |
||
60 | } |
||
61 | |||
62 | if (votes[choice]) { |
||
63 | votes[choice].push(voter); |
||
64 | } else { |
||
65 | votes[choice] = [voter]; |
||
66 | } |
||
67 | |||
68 | return votes; |
||
69 | } |
||
170 |