Conditions | 2 |
Paths | 2 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 | /** |
||
44 | add: function ( e, data ) { |
||
45 | |||
46 | var that = this; |
||
47 | data.id = Date.now(); |
||
48 | |||
49 | var status = $( '<li>' ) |
||
50 | .attr( 'id', data.id ) |
||
51 | .text( data.files[ 0 ].name ); |
||
52 | |||
53 | $( 'ul.fileupload-results', container ).append( status ); |
||
54 | |||
55 | var api = new mw.Api(); |
||
56 | |||
57 | var tokenType = 'csrf'; |
||
58 | |||
59 | if ( mw.config.get( 'wgVersion' ) < '1.27.0' ) { |
||
60 | tokenType = 'edit'; |
||
61 | } |
||
62 | |||
63 | // invalidate cached token; always request a new one |
||
64 | api.badToken( tokenType ); |
||
65 | |||
66 | api.getToken( tokenType ) |
||
67 | .then( |
||
68 | function ( token ) { |
||
69 | |||
70 | data.formData = { |
||
71 | format: 'json', |
||
72 | action: 'upload', |
||
73 | token: token, |
||
74 | ignorewarnings: 1, |
||
75 | text: $( that ).fileupload( 'option', 'text' ), |
||
76 | comment: $( that ).fileupload( 'option', 'comment' ), |
||
77 | filename: data.files[ 0 ].name |
||
78 | }; |
||
79 | |||
80 | data.submit() |
||
81 | .success( function ( result /*, textStatus, jqXHR */ ) { |
||
82 | |||
83 | if ( result.error !== undefined ) { |
||
84 | |||
85 | status.text( status.text() + " ERROR: " + result.error.info ).addClass( 'ful-error api-error' ); |
||
86 | |||
87 | } else { |
||
88 | var link = $( '<a>' ); |
||
89 | link |
||
90 | .attr( 'href', mw.Title.newFromFileName( result.upload.filename ).getUrl() ) |
||
91 | .text( result.upload.filename ); |
||
92 | |||
93 | status |
||
94 | .addClass( 'ful-success' ) |
||
95 | .text( ' OK' ) |
||
96 | .prepend( link ); |
||
97 | } |
||
98 | |||
99 | } ) |
||
100 | .error( function ( /* jqXHR, textStatus, errorThrown */ ) { |
||
101 | status.text( status.text() + " ERROR: Server communication failed." ).addClass( 'ful-error server-error' ); |
||
102 | // console.log( JSON.stringify( arguments ) ); |
||
103 | } ); |
||
104 | }, |
||
105 | function () { |
||
106 | status.text( status.text() + " ERROR: Could not get token." ).addClass( 'ful-error token-error' ); |
||
107 | // console.log( JSON.stringify( arguments ) ); |
||
108 | } |
||
109 | ); |
||
110 | |||
111 | }, |
||
112 | |||
128 |