Conditions | 12 |
Paths | 19 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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:
Complex classes like PassmanImporter.padlock.readFile 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 | /** |
||
42 | return new C_Promise(function(){ |
||
43 | var rows = PassmanImporter.readCsv(file_data, true); |
||
44 | var credential_list = []; |
||
45 | for (var i = 0; i < rows.length; i++) { |
||
46 | var row = rows[i]; |
||
47 | var _credential = PassmanImporter.newCredential(); |
||
48 | var j = 0; |
||
49 | for (var k in row) { |
||
|
|||
50 | if (!row[k]) { |
||
51 | continue; |
||
52 | } |
||
53 | if (k == 'name') { |
||
54 | _credential.label = row.name; |
||
55 | continue; |
||
56 | } |
||
57 | if (k == 'username') { |
||
58 | _credential.username = row.username; |
||
59 | continue; |
||
60 | } |
||
61 | if (k == 'password') { |
||
62 | _credential.password = row.password; |
||
63 | continue; |
||
64 | } |
||
65 | if (k.toLowerCase() == 'url') { |
||
66 | _credential.url = row[k]; |
||
67 | continue; |
||
68 | } |
||
69 | if (k.toLowerCase() == 'e-mail' || |
||
70 | k.toLowerCase() == 'email') { |
||
71 | _credential.email = row[k]; |
||
72 | continue; |
||
73 | } |
||
74 | if (k.toLowerCase() == 'description') { |
||
75 | _credential.description = row[k]; |
||
76 | continue; |
||
77 | } |
||
78 | _credential.custom_fields[j] = { |
||
79 | 'label' : k, |
||
80 | 'value' : row[k], |
||
81 | 'secret' : true, |
||
82 | 'field_type' : 'text' |
||
83 | }; |
||
84 | j++; |
||
85 | } |
||
86 | if(_credential.label){ |
||
87 | credential_list.push(_credential); |
||
88 | } |
||
89 | |||
90 | var progress = { |
||
91 | percent: i/rows.length*100, |
||
92 | loaded: i, |
||
93 | total: rows.length |
||
94 | }; |
||
95 | this.call_progress(progress); |
||
96 | } |
||
97 | this.call_then(credential_list); |
||
98 | }); |
||
99 | }; |
||
101 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: