Conditions | 9 |
Paths | 32 |
Total Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 | /** |
||
4 | function sendPermissions(event) { |
||
5 | // set the icon while storing the values |
||
6 | var icon = document.getElementById('icon_' + this.id); |
||
7 | icon.removeAttribute('class'); |
||
8 | icon.setAttribute('style', 'background: url(../media/system/images/modal/spinner.gif); display: inline-block; width: 16px; height: 16px'); |
||
9 | var iconSuccess = "text-success icon-save fa-save"; |
||
10 | var iconFail = "text-error text-danger icon-remove fa-remove"; |
||
11 | |||
12 | //get values and prepare GET-Parameter |
||
13 | var asset = 'not'; |
||
14 | var component = getUrlParam('component'); |
||
15 | var option = getUrlParam('option'); |
||
16 | var view = getUrlParam('view'); |
||
17 | var title = component; |
||
18 | var value = this.value; |
||
19 | |||
20 | if (option == 'com_redcore' && view == 'config') { |
||
21 | if (component == false) |
||
22 | asset = 'root.1'; |
||
|
|||
23 | else |
||
24 | asset = component; |
||
25 | } |
||
26 | else { |
||
27 | |||
28 | if (document.getElementById('jform_title')) { |
||
29 | title = document.getElementById('jform_title').value; |
||
30 | } |
||
31 | |||
32 | if (component != false && view != false) { |
||
33 | asset = component + '.' + view + '.' + getUrlParam('id'); |
||
34 | } |
||
35 | else if (component == false && view != false) { |
||
36 | asset = option + '.' + view + '.' + getUrlParam('id'); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | var id = this.id.replace('jform_rules_', ''); |
||
41 | var lastUnderscoreIndex = id.lastIndexOf('_'); |
||
42 | |||
43 | var permission_data = { |
||
44 | comp : asset, |
||
45 | action : id.substring(0, lastUnderscoreIndex), |
||
46 | rule : id.substring(lastUnderscoreIndex + 1), |
||
47 | value : value, |
||
48 | title : title |
||
49 | }; |
||
50 | |||
51 | // Remove js messages, if they exist. |
||
52 | Joomla.removeMessages(); |
||
53 | |||
54 | // doing ajax request |
||
55 | jQuery.ajax({ |
||
56 | method : "POST", |
||
57 | url : document.getElementById('permissions-sliders').getAttribute('data-ajaxuri'), |
||
58 | data : permission_data, |
||
59 | datatype : 'json' |
||
60 | }) |
||
61 | .fail(function (jqXHR, textStatus, error) { |
||
62 | // Remove the spinning icon. |
||
63 | icon.removeAttribute('style'); |
||
64 | |||
65 | Joomla.renderMessages(Joomla.ajaxErrorsMessages(jqXHR, textStatus, error)); |
||
66 | |||
67 | icon.setAttribute('class', iconFail); |
||
68 | }) |
||
69 | .done(function (response) { |
||
70 | // Remove the spinning icon. |
||
71 | icon.removeAttribute('style'); |
||
72 | |||
73 | if (response.data) { |
||
74 | // Check if everything is OK |
||
75 | if (response.data.result == true) { |
||
76 | icon.setAttribute('class', iconSuccess); |
||
77 | |||
78 | jQuery(event.target).parents().next('td').find('span') |
||
79 | .removeClass().addClass(response.data.class) |
||
80 | .html(response.data.text); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // Render messages, if any. There are only message in case of errors. |
||
85 | if (typeof response.messages == 'object' && response.messages !== null) |
||
86 | { |
||
87 | Joomla.renderMessages(response.messages); |
||
88 | |||
89 | if (response.data && response.data.result == true) |
||
90 | icon.setAttribute('class', iconSuccess); |
||
91 | else |
||
92 | icon.setAttribute('class', iconFail); |
||
93 | } |
||
94 | }); |
||
95 | } |
||
96 | |||
113 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.