Conditions | 7 |
Paths | 29 |
Total Lines | 84 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 | /* |
||
66 | $this.sendData = function ( e ) { |
||
67 | e.preventDefault(); |
||
68 | |||
69 | // --------------- Check if an ajax request is in precessing |
||
70 | if ( ajaxPending === false ) |
||
71 | ajaxPending = true; |
||
72 | else return; |
||
73 | |||
74 | try { |
||
75 | // --------------- Check if a submit button is found |
||
76 | if ( btnSubmit.length === 0 ) |
||
77 | throw 'Unable to find submit button'; |
||
78 | |||
79 | // --------------- Check if form method is found |
||
80 | if ( method == "" || method === null ) |
||
81 | throw 'Undefined method of form'; |
||
82 | |||
83 | // --------------- Add loader and disabled submit button |
||
84 | btnSubmit |
||
85 | .append( $( settings.icons.loading ).addClass( 'formJS-loading' ) ) |
||
86 | .attr( 'disabled' ); |
||
87 | |||
88 | btnSubmit.addClass( 'disabled' ); |
||
89 | |||
90 | // --------------- Prepare ajax setting |
||
91 | formdata = (window.FormData) ? new FormData( $this[ 0 ] ) : null; |
||
92 | data = (formdata !== null) ? formdata : $this.serialize(); |
||
93 | ajaxSettings = $.extend( settings.form.ajaxSettings, { |
||
94 | url: action, |
||
95 | type: method, |
||
96 | data: data, |
||
97 | processData: false |
||
98 | } ); |
||
99 | |||
100 | $this.trigger( 'formjs:submit', [ ajaxSettings, ajaxPending ] ); |
||
101 | |||
102 | // --------------- Send ajax request |
||
103 | $.ajax( ajaxSettings ) |
||
104 | .done( function ( feedback ) { |
||
105 | try { |
||
106 | // --------------- If no feedback found, write unexpected alert |
||
107 | if ( feedback.length === 0 ) |
||
108 | throw 'No data found on response'; |
||
109 | |||
110 | var feedbackData = $.parseJSON( feedback ); |
||
111 | var notif = ''; |
||
112 | |||
113 | $this.trigger( 'formjs:ajax-success', [ feedback ] ); |
||
114 | |||
115 | // --------------- Check feedback structure |
||
116 | $this.checkFeedbackStructure( feedbackData ); |
||
117 | |||
118 | // --------------- If url field is in feedback, prepare to redirect to it |
||
119 | if ( feedbackData.type === settings.keys.success && feedbackData.hasOwnProperty( 'url' ) ) { |
||
120 | notif = ' - ' + settings.redirection.message; |
||
121 | |||
122 | setTimeout( function () { |
||
123 | window.location.replace( feedbackData.url ); |
||
124 | }, settings.redirection.delay ); |
||
125 | } |
||
126 | |||
127 | // --------------- Make alert object with feedback |
||
128 | currentAlert.type = feedbackData.type; |
||
129 | currentAlert.title = feedbackData.data.title; |
||
130 | currentAlert.message = feedbackData.data.message + notif; |
||
131 | |||
132 | } catch ( error ) { |
||
133 | $this.logError( 'AjaxSuccessCallback', error, feedback ); |
||
134 | } |
||
135 | } ) |
||
136 | .fail( function ( error ) { |
||
137 | $this.logError( 'AjaxFailCallback', error ); |
||
138 | } ) |
||
139 | .always( function () { |
||
140 | // --------------- Call after all ajax request |
||
141 | $this.writeAlert(); |
||
142 | } ); |
||
143 | |||
144 | } catch ( error ) { |
||
145 | // --------------- Call if an error thrown before sending ajax request |
||
146 | $this.logError( 'PreSubmit', error ); |
||
147 | $this.writeAlert(); |
||
148 | } |
||
149 | }; |
||
150 | |||
299 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.