Amir Saboury
December 2016
The pieces of the codes that scream out to be refactored
– Martin Fowler
$('a').find('span').attr('id', 'title').end().click();
move(x1, y1, x2, y2, direction, speed, steps, onFinish)
$.get('/info.json', function (err, data) {
log(data, function (err, data) {
// ...
});
});
function parse(url) {
url = url.split('/');
// ...
}
if (element = stack.pop()){
element.addClass("last");
}
cyclomatic complexity
var download = function (url) {
var callback = function (err, data) {
// this.data = data;
console.log(data);
}.bind(this);
$get(url, callback);
}
var download = function (url) {
var self = this;
var callback = function (err, data) {
self.error = err;
self.data = data;
}
$get(url, callback);
}
* https://sourcemaking.com/refactoring
* https://sourcemaking.com/refactoring
var download = function (url) {
if (url) {
$get(url, function(err, data) {
if (!err) {
if (data.includes('http')) {
//...
}
}
});
}
}
Studied Systems, Data Extraction, Analysis
var a = 42;
var b = 5;
function addA(d) {
return a + d;
}
var c = addA(2) + b;
File
comit
comit
comit
comit
comit
bug inducing
bug fixing
Boolean
Number
Metrics
Code Smells
grunt - nested callbacks
Models how long subjects under observation can survive before the occurrence of an event of interest.
Cox Proportional Hazard Model
Cox Proportional Hazard Model
The number of hours between the previous revision and the revision r.
For each file f and revision r:
The presence of a code smell in the file f in the revision r.
Is the revision r a fault-fixing change?
survfit & coxph
There is no difference between the probability of a fault occurrence in a file containing code smells and a file without code smells.
Null hypotheses:
Request
Less.js
Grunt
Express
Bower
Log-rank Test
(which compares the survival distributions of two samples)
p-value < 0.05
hypothesis rejected
JavaScript files without code smells have hazard rates 65% lower than JavaScript files with code smells and this difference is statistically significant. |
---|
Time, Event
Smelly i
If the file has the code smell type i
LOC, Code Churn, and the number of past bugs
The hazard ratios of the studied code smells vary across the systems
Express
Chained Methods, This Assign, Variable Re-assign, and #Past-bugs
Grunt
Nested Callbacks, Assignment in Conditional Statements, and Variable Re-assign
Bower
Depth, #Past-bugs, and LOC
Less.js
Assignment in Conditional Statements, and #Past-bugs
Request
Variable Re-assign, and #Past-bugs
JavaScript files containing different types of code smells are not equally fault-prone. Developers should consider refactoring files containing "Variable Re-assign" code smell and "Assignment in Conditional Statements" code smell in priority since they seem to increase the risk of faults in the system. |
---|
https://srvy.online
Shared in HackerNews & EchoJS
1484
Participants
92% used JS for client-side applications
51% used JS for server-side applications
68% 3+ years of experience with JavaScript
Nested Callbacks (8.1/10)
Variable Re-assign (6.5/10)
Long Parameter List (6.2/10)
Quantitative study of 5 JavaScript systems
JS files without code smells have hazard rates 65% lower than JavaScript files with code smells
"Variable Re-assign" and "Assignment in Cond. Statements" code smells have the highest hazard rates
Qualitative study with 1484 JS developers
Nested Callbacks, Variable Re-assign, Long Parameter List
Findings bug-fixing commits ("bug", "fix", "defect" etc)
SZZ heuristic
Presence of the smell in the file as a whole
AST provided by ESLint
Logarithmic link function
5 JS Systems
?
Thank you