IDENTIFICATION OF
JAVASCRIPT CLASSES
Shahriar Rostami
Master’sThesis
Supervisor: Dr. NikolaosTsantalis
August 2016
Outline
■ Context of the study (goal, background)
■ Contribution
■ Approach
■ Evaluation of precision and recall
■ Case study
■ Tool features
■ Future work
2
Goal
■ To study existence of classes in JavaScript
■ Try to improve program comprehension
■ Long-term goal: Impact of OOP on maintenance of
JavaScript applications
3
Why JavaScript
■ The most used language on Github (2013-2015)
■ Everywhere: client-side, server-side (Node.JS), libraries, IOT
and databases
■ Dynamic, untyped and interpreted programming language
■ No syntactical support for Classes, Namespaces, and
import/export packages (prior to ES2015)
■ It needs better tooling
4
Class Emulation
f unct i on TheCl ass( ) {
t hi s. f oo = 0;
t hi s. bar = f unct i on( ) {
consol e. l og( t hi s. f oo) ;
}
}
var TheCl ass = f unct i on( ) {
t hi s. f oo = 0;
t hi s. bar = f unct i on( ) {
consol e. l og( t hi s. f oo) ;
}
}
var t heI nst ance = new TheCl ass( ) ;
t heI nst ance. f oo = 2;
t heI nst ance. bar ( ) ; / / 2
A B
C
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
5
Class Emulation
f unct i on TheCl ass ( ) {
t hi s. f oo = 0;
}
TheCl ass. pr ot ot ype. bar = f unct i on( ) {
consol e. l og( t hi s. f oo) ;
}
var TheCl ass = f unct i on( ) {
t hi s. f oo = 0;
}
TheCl ass. pr ot ot ype. bar = f unct i on( ) {
consol e. l og( t hi s. f oo) ;
}
var t heI nst ance = new TheCl ass ( ) ;
t heI nst ance. f oo = 2;
t heI nst ance. bar ( ) ; / / 2
A B
C
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
6
Namespace
(IIFE with return)
var t heCl assI nst ance = new namespace. Publ i cCl ass( ) ; / / I am a cl ass
A B
C
var namespace = ( f unct i on( ) {
var t empl at e = { } ;
t empl at e. myPr oper t y = 0;
t empl at e. Publ i cCl ass = f unct i on( ) {
consol e. l og( ' I am a cl ass' ) ;
}
r et ur n t empl at e;
} ) ( ) ;
var namespace = ( f unct i on( ) {
var cl assDecl ar at i on = f unct i on( ) {
consol e. l og( ' I am a cl ass' ) ;
}
r et ur n {
myPr oper t y : 0,
Publ i cCl ass : cl assDecl ar at i on
} ;
} ) ( ) ;
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
9
1
7
■ To prevent collision
■ To better organize code
Namespace
(IIFE with parameter)
var t heCl assI nst ance = new namespace. Publ i c Cl ass( ) ; / / I am a cl ass
A B
C
var namespace = { } ;
( f unct i on( ns) {
ns. myPr oper t y = 0;
ns. Publ i cCl ass = f unct i on( ) {
consol e. l og( ' I am a cl ass ' ) ;
}
} ) ( namespace) ;
var namespace = { } ;
( f unct i on( ) {
t hi s. myPr oper t y = 0;
t hi s. Publ i cCl ass = f unct i on( ) {
consol e. l og( ' I am a cl ass ' ) ;
}
} ) . appl y( namespace) ;
1
2
3
4
5
6
7
1
2
3
4
5
6
7
1
8
Namespace
(Nested object literals)
var namespace = {
modul e: {
subModul e: {
TheCl ass: f unct i on( ) {
t hi s. f oo = 0;
t hi s. pr i nt I t = f unct i on( ) {
consol e. l og( t hi s. f oo) ;
}
} ,
Anot her Cl ass: f unct i on( ) {
/ / Body of anot her cl ass
}
}
}
} ;
var t heI nst ance = new namespace. modul e. subModul e. TheCl ass( ) ;
t heI nst ance. f oo = 7;
t heI nst ance. pr i nt I t ( ) ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
9
Class and Namespace
■ Class emulation techniques: 4
■ Namespace emulation techniques: 5
■ Total: combination of 20 styles or more…
10
Contributions
■ A technique for identifying class emulations (JSDeodorant)
■ Build an oracle
■ Comparison with the state-of-the-art tool, JSClassFinder
■ Quantitative and Qualitative evaluation of the results to
identify limitations
■ Case study on open source JavaScript programs
11
Approach
12
Identifying Classes
■ Binding object creations: With a light weight data-flow analysis,
match instance creation with function constructor
– Simple Name Identifiers: i.e. new MockWindow()
– Composite Name Identifier: i.e. new namespace.PublicClass()
■ Inferring function constructors: Analyzing the body and
prototype object of functions
– Applies to unresolved functions
– Function with a method or an attribute is inferred as class
13
Unbiased Oracle for Evaluation
■ Using JSDoc Annotations
– @Constructor annotation on a function means that is a class
■ UsingTranspiled Code: automatically transpile projects to vanilla
JS
– TypeScript
– CoffeScript
14
Codes we analyze
15
Project Version
Size
(KLOC)
#JS Files
#Function
s
#Classes
Closure-
library
20160315 605 1,502 23,535 946
Doppio Rev:7229e7d 17.7 47 1,977 154
Atom V1.7.0.beta5 34 116 3,175 102
Precision and Recall
Program
Identified
function
constructors
TP FP FN Precision Recall
Closure-library 1,008 907 101 39 90% 96%
Doppio 154 153 1 1 99% 99%
Atom 106 101 5 1 95% 99%
16
Qualitative analysis
17
■ Two independent evaluators
– Classes that are not inOracle but we found (Extra)
– Classes that we can’t find, but are in Oracle (FN)
■ Limitations:
– Inheritance
– InterfaceVSClass
Comparison with the
State-of-the-art
■ Both tools have high precision on average, but JSDeodorant
greatly outperforms JSClassFinder for recall
18
Project
JSClassFinder[1] JSDeodorant
Precision Recall Precision Recall
Closure-
library
99% 76% 98% 96%
Doppio 96% 14% 99% 99%
Atom 100% 93% 95% 98%
Average 98% 61% 97% 98%
[1]: Silve et.al. Does JavaScript Software Embrace Classes? SANER’15
JSDeodorant is Accurate!
19
JSDeodorant
Precision: 97%
Recall: 98% >
JSClassFinder
Precision: 98%
Recall: 61%
Empirical Study
■ Build a dataset (NodeJS,Website and Library)
■ Preprocess (removal of test and document folder)
■ Any difference of class adaptation among these three groups?
■ Eyeball analysis:Yes
■ Statistically?
20
Analyzed Systems
21
Project #Classes
NodeJS Express 8
Forever 0
Less.js 77
Node-
browserify
2
NPM 35
PM2 11
Statsd 11
Yo 0
Project #Classes
Website Google 128
Facebook 312
Baidu 54
Yahoo! 15
Amazon 124
Twitter 147
Project #Classes Project #Classes
Libraries
Ace 5 Jquery 45
Angular 109 PDF 306
Backbone 18 Underscore 25
Ember 32
The Distribution of Data
■ Normalize number of #classes with #files
■ Appropriate statistical test
■ Shapiro Walk test to assess whether the distribution is normal or
not
■ P-values
– NodeJS: 0.02699
– Websites: 0.0468
– Libraries: 0.01349
22
AppropriateTest?
■ Wilcoxon rank sum test (MannWhitney U test)
■ Statistically significant difference between #Classes?
■ NodeJS <Websites (P-value: 0.0011)
■ Libraries <Websites (P-value: 0.006993)
■ NodeJS < Libraries (P-value: 0.02799)
23
NodeJS < Libraries <Websites
JSDeodorant
■ CLI Mode
– CSV
– Database
24
JSDeodorant
■ Eclipse plugin
– Navigation
– ModuleView
– ModuleVisualization
25
Conclusion
■ An approach for identifying JavaScript classes
– Supporting Namespace, Import/Export
– High precision and Recall
■ An unbiased oracle
■ NodeJS < Libraries <Websites
■ An infrastructure for other studies
■ Eclipse Plugin
26
Future work
■ Finding code smells
– God Class, Data Class, Feature Envy
■ Support for inheritance
■ Transform classes to new version of JavaScript
– ES2015 syntactically supports Class, Namespace and
Import/Export
27
To appear in ICSME’16 ERATrack
28
/sshishe/jsdeodorant

Slides

  • 1.
    IDENTIFICATION OF JAVASCRIPT CLASSES ShahriarRostami Master’sThesis Supervisor: Dr. NikolaosTsantalis August 2016
  • 2.
    Outline ■ Context ofthe study (goal, background) ■ Contribution ■ Approach ■ Evaluation of precision and recall ■ Case study ■ Tool features ■ Future work 2
  • 3.
    Goal ■ To studyexistence of classes in JavaScript ■ Try to improve program comprehension ■ Long-term goal: Impact of OOP on maintenance of JavaScript applications 3
  • 4.
    Why JavaScript ■ Themost used language on Github (2013-2015) ■ Everywhere: client-side, server-side (Node.JS), libraries, IOT and databases ■ Dynamic, untyped and interpreted programming language ■ No syntactical support for Classes, Namespaces, and import/export packages (prior to ES2015) ■ It needs better tooling 4
  • 5.
    Class Emulation f uncti on TheCl ass( ) { t hi s. f oo = 0; t hi s. bar = f unct i on( ) { consol e. l og( t hi s. f oo) ; } } var TheCl ass = f unct i on( ) { t hi s. f oo = 0; t hi s. bar = f unct i on( ) { consol e. l og( t hi s. f oo) ; } } var t heI nst ance = new TheCl ass( ) ; t heI nst ance. f oo = 2; t heI nst ance. bar ( ) ; / / 2 A B C 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 5
  • 6.
    Class Emulation f uncti on TheCl ass ( ) { t hi s. f oo = 0; } TheCl ass. pr ot ot ype. bar = f unct i on( ) { consol e. l og( t hi s. f oo) ; } var TheCl ass = f unct i on( ) { t hi s. f oo = 0; } TheCl ass. pr ot ot ype. bar = f unct i on( ) { consol e. l og( t hi s. f oo) ; } var t heI nst ance = new TheCl ass ( ) ; t heI nst ance. f oo = 2; t heI nst ance. bar ( ) ; / / 2 A B C 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 6
  • 7.
    Namespace (IIFE with return) vart heCl assI nst ance = new namespace. Publ i cCl ass( ) ; / / I am a cl ass A B C var namespace = ( f unct i on( ) { var t empl at e = { } ; t empl at e. myPr oper t y = 0; t empl at e. Publ i cCl ass = f unct i on( ) { consol e. l og( ' I am a cl ass' ) ; } r et ur n t empl at e; } ) ( ) ; var namespace = ( f unct i on( ) { var cl assDecl ar at i on = f unct i on( ) { consol e. l og( ' I am a cl ass' ) ; } r et ur n { myPr oper t y : 0, Publ i cCl ass : cl assDecl ar at i on } ; } ) ( ) ; 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 7 ■ To prevent collision ■ To better organize code
  • 8.
    Namespace (IIFE with parameter) vart heCl assI nst ance = new namespace. Publ i c Cl ass( ) ; / / I am a cl ass A B C var namespace = { } ; ( f unct i on( ns) { ns. myPr oper t y = 0; ns. Publ i cCl ass = f unct i on( ) { consol e. l og( ' I am a cl ass ' ) ; } } ) ( namespace) ; var namespace = { } ; ( f unct i on( ) { t hi s. myPr oper t y = 0; t hi s. Publ i cCl ass = f unct i on( ) { consol e. l og( ' I am a cl ass ' ) ; } } ) . appl y( namespace) ; 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 8
  • 9.
    Namespace (Nested object literals) varnamespace = { modul e: { subModul e: { TheCl ass: f unct i on( ) { t hi s. f oo = 0; t hi s. pr i nt I t = f unct i on( ) { consol e. l og( t hi s. f oo) ; } } , Anot her Cl ass: f unct i on( ) { / / Body of anot her cl ass } } } } ; var t heI nst ance = new namespace. modul e. subModul e. TheCl ass( ) ; t heI nst ance. f oo = 7; t heI nst ance. pr i nt I t ( ) ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 9
  • 10.
    Class and Namespace ■Class emulation techniques: 4 ■ Namespace emulation techniques: 5 ■ Total: combination of 20 styles or more… 10
  • 11.
    Contributions ■ A techniquefor identifying class emulations (JSDeodorant) ■ Build an oracle ■ Comparison with the state-of-the-art tool, JSClassFinder ■ Quantitative and Qualitative evaluation of the results to identify limitations ■ Case study on open source JavaScript programs 11
  • 12.
  • 13.
    Identifying Classes ■ Bindingobject creations: With a light weight data-flow analysis, match instance creation with function constructor – Simple Name Identifiers: i.e. new MockWindow() – Composite Name Identifier: i.e. new namespace.PublicClass() ■ Inferring function constructors: Analyzing the body and prototype object of functions – Applies to unresolved functions – Function with a method or an attribute is inferred as class 13
  • 14.
    Unbiased Oracle forEvaluation ■ Using JSDoc Annotations – @Constructor annotation on a function means that is a class ■ UsingTranspiled Code: automatically transpile projects to vanilla JS – TypeScript – CoffeScript 14
  • 15.
    Codes we analyze 15 ProjectVersion Size (KLOC) #JS Files #Function s #Classes Closure- library 20160315 605 1,502 23,535 946 Doppio Rev:7229e7d 17.7 47 1,977 154 Atom V1.7.0.beta5 34 116 3,175 102
  • 16.
    Precision and Recall Program Identified function constructors TPFP FN Precision Recall Closure-library 1,008 907 101 39 90% 96% Doppio 154 153 1 1 99% 99% Atom 106 101 5 1 95% 99% 16
  • 17.
    Qualitative analysis 17 ■ Twoindependent evaluators – Classes that are not inOracle but we found (Extra) – Classes that we can’t find, but are in Oracle (FN) ■ Limitations: – Inheritance – InterfaceVSClass
  • 18.
    Comparison with the State-of-the-art ■Both tools have high precision on average, but JSDeodorant greatly outperforms JSClassFinder for recall 18 Project JSClassFinder[1] JSDeodorant Precision Recall Precision Recall Closure- library 99% 76% 98% 96% Doppio 96% 14% 99% 99% Atom 100% 93% 95% 98% Average 98% 61% 97% 98% [1]: Silve et.al. Does JavaScript Software Embrace Classes? SANER’15
  • 19.
    JSDeodorant is Accurate! 19 JSDeodorant Precision:97% Recall: 98% > JSClassFinder Precision: 98% Recall: 61%
  • 20.
    Empirical Study ■ Builda dataset (NodeJS,Website and Library) ■ Preprocess (removal of test and document folder) ■ Any difference of class adaptation among these three groups? ■ Eyeball analysis:Yes ■ Statistically? 20
  • 21.
    Analyzed Systems 21 Project #Classes NodeJSExpress 8 Forever 0 Less.js 77 Node- browserify 2 NPM 35 PM2 11 Statsd 11 Yo 0 Project #Classes Website Google 128 Facebook 312 Baidu 54 Yahoo! 15 Amazon 124 Twitter 147 Project #Classes Project #Classes Libraries Ace 5 Jquery 45 Angular 109 PDF 306 Backbone 18 Underscore 25 Ember 32
  • 22.
    The Distribution ofData ■ Normalize number of #classes with #files ■ Appropriate statistical test ■ Shapiro Walk test to assess whether the distribution is normal or not ■ P-values – NodeJS: 0.02699 – Websites: 0.0468 – Libraries: 0.01349 22
  • 23.
    AppropriateTest? ■ Wilcoxon ranksum test (MannWhitney U test) ■ Statistically significant difference between #Classes? ■ NodeJS <Websites (P-value: 0.0011) ■ Libraries <Websites (P-value: 0.006993) ■ NodeJS < Libraries (P-value: 0.02799) 23 NodeJS < Libraries <Websites
  • 24.
    JSDeodorant ■ CLI Mode –CSV – Database 24
  • 25.
    JSDeodorant ■ Eclipse plugin –Navigation – ModuleView – ModuleVisualization 25
  • 26.
    Conclusion ■ An approachfor identifying JavaScript classes – Supporting Namespace, Import/Export – High precision and Recall ■ An unbiased oracle ■ NodeJS < Libraries <Websites ■ An infrastructure for other studies ■ Eclipse Plugin 26
  • 27.
    Future work ■ Findingcode smells – God Class, Data Class, Feature Envy ■ Support for inheritance ■ Transform classes to new version of JavaScript – ES2015 syntactically supports Class, Namespace and Import/Export 27
  • 28.
    To appear inICSME’16 ERATrack 28 /sshishe/jsdeodorant

Editor's Notes

  • #5 Tell story about not having standard But people are using OOP in it And developers made their own patterns Motivation: Whether or not developers are using JavaScript OO practic the long term goalof this research is to see the impact of oop on maintenance
  • #8 Tell why we need namespace -> Collison and Organization
  • #10 Say somethings about object literal
  • #11 I show different way to create class now how should we detetc them
  • #12 Oracle: For precision and recall (evaluate) and comparison and then if it’s good -> case study
  • #13 explain the enhanced model
  • #15 What is a unbiased oracle, what is biased How you build it
  • #16 What is a unbiased oracle, what is biased How you build it
  • #17 TP: chizi ke bayad peyda mikardi, peyda ham kardi. FP: chizi ke nabayad peyda mikardi, vali peyda kardi. FN: chizi ke bayad peyda mikardi, peyda nakardi. TN: chizi ke nabayad peyda mikardi, peyda ham nakardi. Precision: az beine chizayi ke peyda kardi, chandtashoon dorostan? Recall: az beine chizayi ke bayad peyda mikardi chandta ro dorost peyda kardi? Explian the results, talk about qualitative, analysis + limitation (may be in next slide) FP: Az in 101 > 18 tash interface budan (our bug) 82 tash class budan annotation nadashtan FN : No instantiation , nor methods and attribtues
  • #18 For Extra: Consensus whether they are class or not (Some of them for test and lack of @constructor) For FN: lack of implementation like interface, inheritance, empty class
  • #19 JSClassFinder: 1) They can’t infer classes , 2) Because they don’t have dependency, they match each name once!
  • #21 Make a transition slide : Our tool is accurate and out perfom the state of the art so it is reliable to use it for empirical study Why you choose these categories? because other researchers also analyzed these categories [ali mesbah and kim work Also mention for websites which they minify code, JSDeodorant does not break
  • #22 Make a transition slide : Our tool is accurate and out perfom the state of the art so it is reliable to use it for empirical study Why you choose these categories? because other researchers also analyzed these categories [ali mesbah and kim work Also mention for websites which they minify code, JSDeodorant does not break
  • #24 Why NodeJS has less, Why Websites are top Try to normalize with number of statements
  • #25 Making CSVs Database Many flags and so on
  • #26 Explain we have code navigation
  • #29 Link to Github and dataset