Conditions | 1 |
Paths | 1 |
Total Lines | 122 |
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:
1 | /** |
||
239 | function Context (lvl, writer) { |
||
240 | var self = this |
||
241 | var writers |
||
242 | var levels |
||
243 | |||
244 | function reset (lvl, writer) { |
||
245 | writers = new TreeNode(writer || Logger) |
||
246 | levels = new TreeNode(level(lvl) || Level.Info) |
||
247 | } |
||
248 | |||
249 | reset(lvl, writer) |
||
250 | |||
251 | this.reset = reset |
||
252 | |||
253 | function path (name) { |
||
254 | return (name || '').toString().split('.').filter(function (_) { return _ }) |
||
255 | } |
||
256 | |||
257 | this.path = path |
||
258 | |||
259 | /** |
||
260 | * Retrieves level for provided logger |
||
261 | * |
||
262 | * @param {string} name |
||
263 | * @return {Level} |
||
264 | */ |
||
265 | this.getLevel = function (name) { return levels.retrieve(path(name)) } |
||
266 | |||
267 | /** |
||
268 | * Retrieves level for provided logger |
||
269 | * |
||
270 | * @deprecated |
||
271 | * |
||
272 | * @param {string} name |
||
273 | * @return {Level} |
||
274 | */ |
||
275 | this.getThreshold = this.getLevel |
||
276 | |||
277 | /** |
||
278 | * Sets level for specified logger |
||
279 | * |
||
280 | * @param {string} [name] |
||
281 | * @param {Level} lvl |
||
282 | */ |
||
283 | this.setLevel = function (name, lvl) { |
||
284 | if (!lvl) { |
||
285 | lvl = name |
||
286 | name = null |
||
287 | } |
||
288 | levels.put(path(name), level(lvl)) |
||
289 | return self |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Sets level for specified logger |
||
294 | * |
||
295 | * @deprecated |
||
296 | * |
||
297 | * @param {string} [name] |
||
298 | * @param {Level} level |
||
299 | */ |
||
300 | this.setThreshold = this.setLevel |
||
301 | |||
302 | /** |
||
303 | * Removes and returns level at provided path |
||
304 | * |
||
305 | * @param {string} name |
||
306 | * @return {Level|undefined} |
||
307 | */ |
||
308 | this.removeLevel = function (name) { |
||
309 | var segments = path(name) |
||
310 | if (segments.length === 0) { |
||
311 | throw new Error('You can\'t remove default writer') |
||
312 | } |
||
313 | return levels.remove(segments) |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param {string} name |
||
318 | * |
||
319 | * @return {IWritable} |
||
320 | */ |
||
321 | this.getWriter = function (name) { return writers.retrieve(path(name)) } |
||
322 | |||
323 | /** |
||
324 | * @param {string} [name] Logger name |
||
325 | * @param {IWritable} writer |
||
326 | */ |
||
327 | this.setWriter = function (name, writer) { |
||
328 | if (!writer) { |
||
329 | writer = name |
||
330 | name = null |
||
331 | } |
||
332 | writers.put(path(name), writer) |
||
333 | return self |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Removes and returns writer under provided path |
||
338 | * |
||
339 | * @throws If attempted to remove root writer |
||
340 | * |
||
341 | * @param {string} name |
||
342 | * @return {IWritable|undefined} |
||
343 | */ |
||
344 | this.removeWriter = function (name) { |
||
345 | var segments = path(name) |
||
346 | if (segments.length === 0) { |
||
347 | throw new Error('You can\'t remove default writer') |
||
348 | } |
||
349 | return writers.remove(segments) |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @abstract |
||
354 | * @function Context#create |
||
355 | * @param {string} name |
||
356 | * @param {Level} [threshold] |
||
357 | * @param {IWritable} [writer] |
||
358 | * @return {T} |
||
359 | */ |
||
360 | } |
||
361 | |||
373 |