| Conditions | 20 |
| Total Lines | 60 |
| Code Lines | 38 |
| Lines | 60 |
| Ratio | 100 % |
| 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 session.js ➔ updateSession 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 | View Code Duplication | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); |
|
| 49 | function updateSession(session, context = {}) { |
||
| 50 | if (context.user) { |
||
| 51 | if (!session.ipAddress && context.user.ip_address) { |
||
| 52 | session.ipAddress = context.user.ip_address; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (!session.did && !context.did) { |
||
| 56 | session.did = context.user.id || context.user.email || context.user.username; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | session.timestamp = context.timestamp || time.timestampInSeconds(); |
||
| 61 | |||
| 62 | if (context.abnormal_mechanism) { |
||
| 63 | session.abnormal_mechanism = context.abnormal_mechanism; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (context.ignoreDuration) { |
||
| 67 | session.ignoreDuration = context.ignoreDuration; |
||
| 68 | } |
||
| 69 | if (context.sid) { |
||
| 70 | // Good enough uuid validation. — Kamil |
||
| 71 | session.sid = context.sid.length === 32 ? context.sid : misc.uuid4(); |
||
| 72 | } |
||
| 73 | if (context.init !== undefined) { |
||
| 74 | session.init = context.init; |
||
| 75 | } |
||
| 76 | if (!session.did && context.did) { |
||
| 77 | session.did = `${context.did}`; |
||
| 78 | } |
||
| 79 | if (typeof context.started === 'number') { |
||
| 80 | session.started = context.started; |
||
| 81 | } |
||
| 82 | if (session.ignoreDuration) { |
||
| 83 | session.duration = undefined; |
||
| 84 | } else if (typeof context.duration === 'number') { |
||
| 85 | session.duration = context.duration; |
||
| 86 | } else { |
||
| 87 | const duration = session.timestamp - session.started; |
||
| 88 | session.duration = duration >= 0 ? duration : 0; |
||
| 89 | } |
||
| 90 | if (context.release) { |
||
| 91 | session.release = context.release; |
||
| 92 | } |
||
| 93 | if (context.environment) { |
||
| 94 | session.environment = context.environment; |
||
| 95 | } |
||
| 96 | if (!session.ipAddress && context.ipAddress) { |
||
| 97 | session.ipAddress = context.ipAddress; |
||
| 98 | } |
||
| 99 | if (!session.userAgent && context.userAgent) { |
||
| 100 | session.userAgent = context.userAgent; |
||
| 101 | } |
||
| 102 | if (typeof context.errors === 'number') { |
||
| 103 | session.errors = context.errors; |
||
| 104 | } |
||
| 105 | if (context.status) { |
||
| 106 | session.status = context.status; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 166 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.