Conditions | 1 |
Paths | 1 |
Total Lines | 91 |
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 | /** |
||
5 | (function($, Symphony) { |
||
|
|||
6 | |||
7 | /** |
||
8 | * Fills the target input/textarea with a value from the source element. |
||
9 | * The plugin cease to change the value when the target is edited by the user and has a value. |
||
10 | * |
||
11 | * @name $.symphonyDefaultValue |
||
12 | * @class |
||
13 | * |
||
14 | * @param {Object} options An object specifying containing the attributes specified below |
||
15 | * @param {String} [options.sourceElement='.js-defaultvalue-source'] Selector to find the default value |
||
16 | * @param {String} [options.sourceEvent='select'] The event that triggers setting the value in the target element |
||
17 | * @param {String} [options.targetEvent='keyup blur'] The event(s) to watch for user interaction |
||
18 | * |
||
19 | * @example |
||
20 | |||
21 | $('.js-defaultvalue-target').symphonyDefaultValue(); |
||
22 | */ |
||
23 | $.fn.symphonyDefaultValue = function(options) { |
||
24 | var objects = this, |
||
25 | isOn = false, |
||
26 | settings = { |
||
27 | sourceElement: '.js-defaultvalue-source', |
||
28 | sourceEvent: 'change', |
||
29 | targetEvent: 'keyup blur' |
||
30 | }; |
||
31 | |||
32 | $.extend(settings, options); |
||
33 | |||
34 | // append our namespace on the sourceEvent |
||
35 | settings.sourceEvent += '.symphony-defaultvalue'; |
||
36 | |||
37 | var source = $(settings.sourceElement); |
||
38 | |||
39 | var getTargetValue = function () { |
||
40 | return objects.val(); |
||
41 | }; |
||
42 | |||
43 | var setTargetValue = function (val) { |
||
44 | objects.val(val); |
||
45 | }; |
||
46 | |||
47 | var getSourceValue = function () { |
||
48 | return source.find('option:selected').text(); |
||
49 | }; |
||
50 | |||
51 | var sourceChanged = function (e) { |
||
52 | if (isOn) { |
||
53 | setTargetValue(getSourceValue()); |
||
54 | } |
||
55 | }; |
||
56 | |||
57 | var on = function () { |
||
58 | if (isOn) { |
||
59 | return; |
||
60 | } |
||
61 | source.on(settings.sourceEvent, sourceChanged); |
||
62 | isOn = true; |
||
63 | }; |
||
64 | |||
65 | var off = function () { |
||
66 | if (!isOn) { |
||
67 | return; |
||
68 | } |
||
69 | $(settings.sourceElement).off(settings.sourceEvent); |
||
70 | isOn = false; |
||
71 | }; |
||
72 | |||
73 | /*------------------------------------------------------------------------- |
||
74 | Initialisation |
||
75 | -------------------------------------------------------------------------*/ |
||
76 | |||
77 | objects.on(settings.targetEvent, function (e) { |
||
78 | if (!getTargetValue()) { |
||
79 | on(); |
||
80 | } |
||
81 | else { |
||
82 | off(); |
||
83 | } |
||
84 | }); |
||
85 | |||
86 | if (!getTargetValue()) { |
||
87 | on(); |
||
88 | } |
||
89 | |||
90 | /*-----------------------------------------------------------------------*/ |
||
91 | |||
92 | return objects; |
||
93 | }; |
||
94 | |||
95 | })(window.jQuery, window.Symphony); |
||
96 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.