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