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 | import { uuid4 } from './utils/misc.js'; |
|
47 | function updateSession(session, context = {}) { |
||
48 | if (context.user) { |
||
49 | if (!session.ipAddress && context.user.ip_address) { |
||
50 | session.ipAddress = context.user.ip_address; |
||
51 | } |
||
52 | |||
53 | if (!session.did && !context.did) { |
||
54 | session.did = context.user.id || context.user.email || context.user.username; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | session.timestamp = context.timestamp || timestampInSeconds(); |
||
59 | |||
60 | if (context.abnormal_mechanism) { |
||
61 | session.abnormal_mechanism = context.abnormal_mechanism; |
||
62 | } |
||
63 | |||
64 | if (context.ignoreDuration) { |
||
65 | session.ignoreDuration = context.ignoreDuration; |
||
66 | } |
||
67 | if (context.sid) { |
||
68 | // Good enough uuid validation. — Kamil |
||
69 | session.sid = context.sid.length === 32 ? context.sid : uuid4(); |
||
70 | } |
||
71 | if (context.init !== undefined) { |
||
72 | session.init = context.init; |
||
73 | } |
||
74 | if (!session.did && context.did) { |
||
75 | session.did = `${context.did}`; |
||
76 | } |
||
77 | if (typeof context.started === 'number') { |
||
78 | session.started = context.started; |
||
79 | } |
||
80 | if (session.ignoreDuration) { |
||
81 | session.duration = undefined; |
||
82 | } else if (typeof context.duration === 'number') { |
||
83 | session.duration = context.duration; |
||
84 | } else { |
||
85 | const duration = session.timestamp - session.started; |
||
86 | session.duration = duration >= 0 ? duration : 0; |
||
87 | } |
||
88 | if (context.release) { |
||
89 | session.release = context.release; |
||
90 | } |
||
91 | if (context.environment) { |
||
92 | session.environment = context.environment; |
||
93 | } |
||
94 | if (!session.ipAddress && context.ipAddress) { |
||
95 | session.ipAddress = context.ipAddress; |
||
96 | } |
||
97 | if (!session.userAgent && context.userAgent) { |
||
98 | session.userAgent = context.userAgent; |
||
99 | } |
||
100 | if (typeof context.errors === 'number') { |
||
101 | session.errors = context.errors; |
||
102 | } |
||
103 | if (context.status) { |
||
104 | session.status = context.status; |
||
105 | } |
||
106 | } |
||
107 | |||
162 |