| Conditions | 17 |
| Total Lines | 53 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 version-compare.js ➔ versionCompare 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 | /* |
||
| 39 | function versionCompare(v1, v2, options) { |
||
| 40 | const lexicographical = (options && options.lexicographical) || false, |
||
| 41 | zeroExtend = (options && options.zeroExtend) || true; |
||
| 42 | |||
| 43 | let v1parts = (v1 || "0").split('.'), |
||
| 44 | v2parts = (v2 || "0").split('.'); |
||
| 45 | |||
| 46 | function isValidPart(x) { |
||
| 47 | return (lexicographical ? /^\d+[A-Za-zαß]*$/ : /^\d+[A-Za-zαß]?$/).test(x); |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) { |
||
| 51 | return NaN; |
||
| 52 | } |
||
| 53 | |||
| 54 | if (zeroExtend) { |
||
| 55 | while (v1parts.length < v2parts.length) v1parts.push("0"); |
||
| 56 | while (v2parts.length < v1parts.length) v2parts.push("0"); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (!lexicographical) { |
||
| 60 | v1parts = v1parts.map(function(x){ |
||
| 61 | const match = (/[A-Za-zαß]/).exec(x); |
||
| 62 | return Number(match ? x.replace(match[0], "." + x.charCodeAt(match.index)):x); |
||
| 63 | }); |
||
| 64 | v2parts = v2parts.map(function(x){ |
||
| 65 | const match = (/[A-Za-zαß]/).exec(x); |
||
| 66 | return Number(match ? x.replace(match[0], "." + x.charCodeAt(match.index)):x); |
||
| 67 | }); |
||
| 68 | } |
||
| 69 | |||
| 70 | for (let i = 0; i < v1parts.length; ++i) { |
||
| 71 | if (v2parts.length === i) { |
||
| 72 | return 1; |
||
| 73 | } |
||
| 74 | |||
| 75 | if (v1parts[i] === v2parts[i]) { |
||
| 76 | continue; |
||
|
|
|||
| 77 | } |
||
| 78 | else if (v1parts[i] > v2parts[i]) { |
||
| 79 | return 1; |
||
| 80 | } |
||
| 81 | else { |
||
| 82 | return -1; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if (v1parts.length !== v2parts.length) { |
||
| 87 | return -1; |
||
| 88 | } |
||
| 89 | |||
| 90 | return 0; |
||
| 91 | } |