| Conditions | 17 |
| Paths | 98 |
| Total Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| 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:
Complex classes like load-image-orientation.js ➔ ... ➔ loadImage.getTransformedOptions 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 | /* |
||
| 113 | loadImage.getTransformedOptions = function (img, opts, data) { |
||
| 114 | var options = originalGetTransformedOptions.call(loadImage, img, opts) |
||
| 115 | var orientation = options.orientation |
||
| 116 | var newOptions |
||
| 117 | var i |
||
| 118 | if (orientation === true && data && data.exif) { |
||
| 119 | orientation = data.exif.get('Orientation') |
||
| 120 | } |
||
| 121 | if (!orientation || orientation > 8 || orientation === 1) { |
||
| 122 | return options |
||
| 123 | } |
||
| 124 | newOptions = {} |
||
| 125 | for (i in options) { |
||
| 126 | if (options.hasOwnProperty(i)) { |
||
| 127 | newOptions[i] = options[i] |
||
| 128 | } |
||
| 129 | } |
||
| 130 | newOptions.orientation = orientation |
||
| 131 | switch (orientation) { |
||
| 132 | case 2: |
||
| 133 | // horizontal flip |
||
| 134 | newOptions.left = options.right |
||
| 135 | newOptions.right = options.left |
||
| 136 | break |
||
| 137 | case 3: |
||
| 138 | // 180° rotate left |
||
| 139 | newOptions.left = options.right |
||
| 140 | newOptions.top = options.bottom |
||
| 141 | newOptions.right = options.left |
||
| 142 | newOptions.bottom = options.top |
||
| 143 | break |
||
| 144 | case 4: |
||
| 145 | // vertical flip |
||
| 146 | newOptions.top = options.bottom |
||
| 147 | newOptions.bottom = options.top |
||
| 148 | break |
||
| 149 | case 5: |
||
| 150 | // vertical flip + 90 rotate right |
||
| 151 | newOptions.left = options.top |
||
| 152 | newOptions.top = options.left |
||
| 153 | newOptions.right = options.bottom |
||
| 154 | newOptions.bottom = options.right |
||
| 155 | break |
||
| 156 | case 6: |
||
| 157 | // 90° rotate right |
||
| 158 | newOptions.left = options.top |
||
| 159 | newOptions.top = options.right |
||
| 160 | newOptions.right = options.bottom |
||
| 161 | newOptions.bottom = options.left |
||
| 162 | break |
||
| 163 | case 7: |
||
| 164 | // horizontal flip + 90 rotate right |
||
| 165 | newOptions.left = options.bottom |
||
| 166 | newOptions.top = options.right |
||
| 167 | newOptions.right = options.top |
||
| 168 | newOptions.bottom = options.left |
||
| 169 | break |
||
| 170 | case 8: |
||
| 171 | // 90° rotate left |
||
| 172 | newOptions.left = options.bottom |
||
| 173 | newOptions.top = options.left |
||
| 174 | newOptions.right = options.top |
||
| 175 | newOptions.bottom = options.right |
||
| 176 | break |
||
| 177 | } |
||
| 178 | if (newOptions.orientation > 4) { |
||
| 179 | newOptions.maxWidth = options.maxHeight |
||
| 180 | newOptions.maxHeight = options.maxWidth |
||
| 181 | newOptions.minWidth = options.minHeight |
||
| 182 | newOptions.minHeight = options.minWidth |
||
| 183 | newOptions.sourceWidth = options.sourceHeight |
||
| 184 | newOptions.sourceHeight = options.sourceWidth |
||
| 185 | } |
||
| 186 | return newOptions |
||
| 187 | } |
||
| 188 | }) |
||
| 189 |