Conditions | 13 |
Total Lines | 65 |
Code Lines | 39 |
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 {object} votes - Total votes cast in the poll |
||
11 | * @param {boolean} isAnonymous - save name or not |
||
12 | * @param {number} maxVotes - save name or not |
||
13 | * @returns {Votes} Map of cast votes keyed by choice index |
||
14 | */ |
||
15 | export function saveVotes(choice: number, voter: Voter, votes: Votes, isAnonymous = false, maxVotes = 1) { |
||
16 | if (maxVotes === 1) { |
||
17 | Object.keys(votes).forEach(function(choiceIndex) { |
||
18 | if (votes[choiceIndex]) { |
||
19 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
20 | if (existed > -1) { |
||
21 | votes[choiceIndex].splice(existed, 1); |
||
22 | } |
||
23 | } |
||
24 | }); |
||
25 | } else { |
||
26 | // get current voter total vote |
||
27 | let voteCount = 0; |
||
28 | let voted = false; |
||
29 | Object.keys(votes).forEach(function(choiceIndex) { |
||
30 | if (votes[choiceIndex]) { |
||
31 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
32 | if (existed > -1) { |
||
33 | voteCount += 1; |
||
34 | } |
||
35 | if (existed > -1 && parseInt(choiceIndex) === choice) { |
||
36 | voted = true; |
||
37 | } |
||
38 | } |
||
39 | }); |
||
40 | if (voteCount >= maxVotes || voted) { |
||
41 | let deleted = false; |
||
42 | Object.keys(votes).forEach(function(choiceIndex) { |
||
43 | if (votes[choiceIndex]) { |
||
44 | const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid); |
||
45 | if (((voteCount >= maxVotes && existed > -1 && !voted) || |
||
46 | (voted && parseInt(choiceIndex) === choice)) && !deleted) { |
||
47 | votes[choiceIndex].splice(existed, 1); |
||
48 | deleted = true; |
||
49 | } |
||
50 | } |
||
51 | }); |
||
52 | } |
||
53 | if (voted) { |
||
54 | return votes; |
||
55 | } |
||
56 | } |
||
57 | if (isAnonymous) { |
||
58 | delete voter.name; |
||
59 | } |
||
60 | |||
61 | if (votes[choice]) { |
||
62 | votes[choice].push(voter); |
||
63 | } else { |
||
64 | votes[choice] = [voter]; |
||
65 | } |
||
66 | |||
67 | return votes; |
||
68 | } |
||
169 |