Conditions | 1 |
Paths | 1 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 1 |
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:
1 | /** |
||
8 | 1 | const Movies = () => { |
|
9 | 1 | const __ = { |
|
10 | params: {}, |
||
11 | |||
12 | requestAPI: function (url) { |
||
13 | 6 | return request({ |
|
14 | uri: url, |
||
15 | qs: this.params, |
||
16 | json: true |
||
17 | }) |
||
18 | } |
||
19 | } |
||
20 | |||
21 | 1 | const proto = { |
|
22 | endpoint: { |
||
23 | list: 'https://yts.ag/api/v2/list_movies.json', |
||
24 | details: 'https://yts.ag/api/v2/movie_details.json' |
||
25 | }, |
||
26 | |||
27 | setEndpoint: function (endpoint) { |
||
28 | 3 | if (typeof endpoint !== 'object') { |
|
29 | 1 | throw new Error('endpoint must be an object') |
|
30 | } |
||
31 | |||
32 | 2 | for (let i in endpoint) { |
|
33 | 2 | this.endpoint[i] = endpoint[i] |
|
34 | } |
||
35 | |||
36 | 2 | return this |
|
37 | }, |
||
38 | |||
39 | setParams: function (params) { |
||
40 | 6 | if (typeof params !== 'object') { |
|
41 | 1 | throw new Error('params must be an object') |
|
42 | } |
||
43 | |||
44 | 5 | __.params = {} |
|
45 | |||
46 | 5 | for (let i in params) { |
|
47 | 7 | __.params[i] = params[i] |
|
48 | } |
||
49 | |||
50 | 5 | return this |
|
51 | }, |
||
52 | |||
53 | get: function () { |
||
54 | 5 | return __.requestAPI(this.endpoint.list) |
|
55 | }, |
||
56 | |||
57 | findByGenre: function (genre) { |
||
58 | 1 | return this.setParams({genres: genre}).get() |
|
59 | }, |
||
60 | |||
61 | findByQuality: function (quality) { |
||
62 | 1 | return this.setParams({quality: quality}).get() |
|
63 | }, |
||
64 | |||
65 | findByRating: function (rating) { |
||
66 | 1 | return this.setParams({minimum_rating: rating}).get() |
|
67 | }, |
||
68 | |||
69 | search: function (term) { |
||
70 | 1 | return this.setParams({query_term: term}).get() |
|
71 | }, |
||
72 | |||
73 | findByID: function (id) { |
||
74 | 1 | this.setParams({ |
|
75 | movie_id: id, |
||
76 | with_images: true, |
||
77 | with_cast: true |
||
78 | }) |
||
79 | |||
80 | 1 | return __.requestAPI(this.endpoint.details) |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 1 | return Object.create(proto) |
|
85 | } |
||
86 | |||
88 |