Conditions | 24 |
Paths | 5761 |
Total Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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:
Complex classes like tableWidget.js ➔ sortTable 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 | /* |
||
77 | function sortTable() { |
||
78 | if (!tableWidget_okToSort)return; |
||
79 | tableWidget_okToSort = false; |
||
80 | /* Getting index of current column */ |
||
81 | var obj = this; |
||
82 | var indexThis = 0; |
||
83 | while (obj.previousSibling) { |
||
84 | obj = obj.previousSibling; |
||
85 | if (obj.tagName == 'TD')indexThis++; |
||
86 | } |
||
87 | var images = this.getElementsByTagName('IMG'); |
||
88 | |||
89 | if (this.getAttribute('direction') || this.direction) { |
||
90 | direction = this.getAttribute('direction'); |
||
91 | if (navigator.userAgent.indexOf('Opera') >= 0)direction = this.direction; |
||
92 | if (direction == 'ascending') { |
||
93 | direction = 'descending'; |
||
94 | this.setAttribute('direction', 'descending'); |
||
95 | this.direction = 'descending'; |
||
96 | } else { |
||
97 | direction = 'ascending'; |
||
98 | this.setAttribute('direction', 'ascending'); |
||
99 | this.direction = 'ascending'; |
||
100 | } |
||
101 | } else { |
||
102 | direction = 'ascending'; |
||
103 | this.setAttribute('direction', 'ascending'); |
||
104 | this.direction = 'ascending'; |
||
105 | } |
||
106 | |||
107 | |||
108 | if (direction == 'descending') { |
||
109 | images[0].style.display = 'inline'; |
||
110 | images[0].style.visibility = 'visible'; |
||
111 | images[1].style.display = 'none'; |
||
112 | } else { |
||
113 | images[1].style.display = 'inline'; |
||
114 | images[1].style.visibility = 'visible'; |
||
115 | images[0].style.display = 'none'; |
||
116 | } |
||
117 | |||
118 | |||
119 | var tableObj = this.parentNode.parentNode.parentNode; |
||
120 | var tBody = tableObj.getElementsByTagName('TBODY')[0]; |
||
121 | |||
122 | var widgetIndex = tableObj.id.replace(/[^\d]/g, ''); |
||
123 | var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String |
||
124 | if (activeColumn[widgetIndex] && activeColumn[widgetIndex] != this) { |
||
125 | var images = activeColumn[widgetIndex].getElementsByTagName('IMG'); |
||
126 | images[0].style.display = 'none'; |
||
127 | images[1].style.display = 'inline'; |
||
128 | images[1].style.visibility = 'hidden'; |
||
129 | if (activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction'); |
||
130 | } |
||
131 | |||
132 | activeColumn[widgetIndex] = this; |
||
133 | |||
134 | var cellArray = new Array(); |
||
135 | var cellObjArray = new Array(); |
||
136 | for (var no = 1; no < tableObj.rows.length; no++) { |
||
137 | var content = tableObj.rows[no].cells[indexThis].innerHTML + ''; |
||
138 | cellArray.push(content); |
||
139 | cellObjArray.push(tableObj.rows[no].cells[indexThis]); |
||
140 | } |
||
141 | |||
142 | if (sortMethod == 'N') { |
||
143 | cellArray = cellArray.sort(sortNumeric); |
||
144 | } else { |
||
145 | cellArray = cellArray.sort(sortString); |
||
146 | } |
||
147 | |||
148 | if (direction == 'descending') { |
||
149 | for (var no = cellArray.length; no >= 0; no--) { |
||
150 | for (var no2 = 0; no2 < cellObjArray.length; no2++) { |
||
151 | if (cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')) { |
||
152 | cellObjArray[no2].setAttribute('allreadySorted', '1'); |
||
153 | tBody.appendChild(cellObjArray[no2].parentNode); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } else { |
||
158 | for (var no = 0; no < cellArray.length; no++) { |
||
159 | for (var no2 = 0; no2 < cellObjArray.length; no2++) { |
||
160 | if (cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')) { |
||
161 | cellObjArray[no2].setAttribute('allreadySorted', '1'); |
||
162 | tBody.appendChild(cellObjArray[no2].parentNode); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | for (var no2 = 0; no2 < cellObjArray.length; no2++) { |
||
169 | cellObjArray[no2].removeAttribute('allreadySorted'); |
||
170 | } |
||
171 | |||
172 | tableWidget_okToSort = true; |
||
173 | |||
174 | |||
175 | } |
||
176 | |||
287 |