Conditions | 1 |
Paths | 1 |
Total Lines | 74 |
Code Lines | 47 |
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:
1 | import { |
||
8 | describe('VisitsParser', () => { |
||
9 | const visits = [ |
||
10 | { |
||
11 | userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0', |
||
12 | referer: 'https://google.com', |
||
13 | visitLocation: { |
||
14 | countryName: 'Spain', |
||
15 | }, |
||
16 | }, |
||
17 | { |
||
18 | userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0', |
||
19 | referer: 'https://google.com', |
||
20 | visitLocation: { |
||
21 | countryName: 'United States', |
||
22 | }, |
||
23 | }, |
||
24 | { |
||
25 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', |
||
26 | visitLocation: { |
||
27 | countryName: 'Spain', |
||
28 | }, |
||
29 | }, |
||
30 | { |
||
31 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', |
||
32 | referer: 'https://m.facebook.com', |
||
33 | visitLocation: { |
||
34 | countryName: 'Spain', |
||
35 | }, |
||
36 | }, |
||
37 | { |
||
38 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41', |
||
39 | }, |
||
40 | ]; |
||
41 | |||
42 | describe('processOsStats', () => { |
||
43 | it('properly parses OS stats', () => { |
||
44 | expect(processOsStats(visits)).toEqual({ |
||
45 | Linux: 3, |
||
46 | Windows: 1, |
||
47 | MacOS: 1, |
||
48 | }); |
||
49 | }); |
||
50 | }); |
||
51 | |||
52 | describe('processBrowserStats', () => { |
||
53 | it('properly parses browser stats', () => { |
||
54 | expect(processBrowserStats(visits)).toEqual({ |
||
55 | Firefox: 2, |
||
56 | Chrome: 2, |
||
57 | Opera: 1, |
||
58 | }); |
||
59 | }); |
||
60 | }); |
||
61 | |||
62 | describe('processReferrersStats', () => { |
||
63 | it('properly parses referrer stats', () => { |
||
64 | expect(processReferrersStats(visits)).toEqual({ |
||
65 | 'Unknown': 2, |
||
66 | 'google.com': 2, |
||
67 | 'm.facebook.com': 1, |
||
68 | }); |
||
69 | }); |
||
70 | }); |
||
71 | |||
72 | describe('processCountriesStats', () => { |
||
73 | it('properly parses countries stats', () => { |
||
74 | expect(processCountriesStats(visits)).toEqual({ |
||
75 | 'Spain': 3, |
||
76 | 'United States': 1, |
||
77 | 'Unknown': 1, |
||
78 | }); |
||
79 | }); |
||
80 | }); |
||
81 | }); |
||
82 |