don t call us we ll call you
play

Don't Call Us, We'll Call You: Characterizing Callbacks in - PowerPoint PPT Presentation

Don't Call Us, We'll Call You: Characterizing Callbacks in JavaScript Keheliya Gallaba, Ali Mesbah, Ivan Beschastnikh University of British Columbia 1 Why JavaScript? On the client 2 Why JavaScript? On the client On the server 3 Why


  1. Don't Call Us, We'll Call You: Characterizing Callbacks in JavaScript Keheliya Gallaba, Ali Mesbah, Ivan Beschastnikh University of British Columbia 1

  2. Why JavaScript? On the client 2

  3. Why JavaScript? On the client On the server 3

  4. Why JavaScript? On the client On the server Even in hardware! 4

  5. Why JavaScript? JavaScript has become the modern lingua franca! On the client On the server Even in hardware! 5

  6. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 6

  7. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 7

  8. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 8

  9. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 9

  10. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 10

  11. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 11

  12. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ 6. eventHandler() � 12

  13. � � � JavaScript Event-driven Model Time 1. log( 'Hello’ ) console .log( 'Hello' ); � 2. Do HTTP get call � 3. Register event Handler � $ .get( 'data.json' , callback ); � 4. log( ’World’ ) $ ( "button" ).click(eventHandler); � 5. callback() � console .log( 'World' ); ¡ eventHandler() � 13

  14. Why Callbacks? $ ( "button" ).click( buttonHandler ); ¡ ¡ function ¡ buttonHandler (event){ ¡ ¡ ¡ ¡ ¡alert( "Button ¡Clicked." ); ¡ } ¡ Used For: ○ HTTP Request/Response ○ File I/O in Node.js ○ Mouse click/drag events in the browser 14

  15. Why Callbacks? $ ( "button" ).click( buttonHandler ); ¡ ¡ function ¡ buttonHandler (event){ ¡ ¡ ¡ ¡ ¡alert( "Button ¡Clicked." ); ¡ } ¡ Used For: ○ HTTP Request/Response ○ File I/O in Node.js ○ Mouse click/drag events in the browser You cannot write an interesting program in JavaScript without using callbacks! 15

  16. Motivation ● Callbacks are important in all non-trivial JS programs. ● But, how do developers use callbacks? ● No prior research on this topic. Goal of this work 16

  17. Outline Motivation ✔ Methodology & Overview Characterizing Problems Anonymous Callbacks Asynchronous Callbacks Nested Callbacks Characterizing Solutions Error-first Protocol Async.js Promises Conclusion 17

  18. Outline Motivation ✔ ► Methodology & Overview Characterizing Problems Anonymous Callbacks Asynchronous Callbacks Nested Callbacks Characterizing Solutions Error-first Protocol Async.js Promises Conclusion 18

  19. Subject Systems 138 popular open source JavaScript subject systems, from 6 distinct categories, with both Client-side & Server-side code. 19

  20. What is a callback? A callback is a function that is passed as an argument to another function, which is expected to invoke it either immediately or at some point function ¡getInput ¡(options, ¡callback) ¡{ ¡ in the future . ¡ ¡ ¡allUserData.push ¡(options); ¡ ¡ ¡ ¡callback ¡(options); ¡ } ¡ var ¡logStuff ¡= ¡function ¡() ¡{ ¡... ¡} ¡ getInput ¡({name:"Rich", ¡speciality:"JavaScript"}, ¡logStuff); ¡ 20

  21. What is a callback? Accepts a callback A callback is a function that is passed as an argument to another function, which is expected to invoke it either immediately or at some point function ¡getInput ¡(options, ¡callback) ¡{ ¡ in the future . ¡ ¡ ¡allUserData.push ¡(options); ¡ Invokes the callback ¡ ¡ ¡callback ¡(options); ¡ } ¡ var ¡logStuff ¡= ¡function ¡() ¡{ ¡... ¡} ¡ getInput ¡({name:"Rich", ¡speciality:"JavaScript"}, ¡logStuff); ¡ Passing a callback as an argument to a function 21

  22. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 22

  23. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 23

  24. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 24

  25. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 25

  26. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 1 26

  27. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 1 27

  28. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 1 28

  29. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 1 29

  30. Detecting callbacks: An example getRecord() accepts logStuff() as a callback because there exist a path... getRecord(cb) ¡ ➡ ¡http.get() ¡ ➡ ¡Anonymous() ¡ ➡ ¡logStuff() ¡ 1 30

  31. How prevalent are callbacks? Callback-accepting function definitions ● On average, 10% of all function definitions take callback arguments. ● They are more prevalent in server-side code (10%) than in client- side code (4.5%) . Callback-accepting function call-sites ● 19% of all function callsites take callback arguments. ● Callback-accepting function call-sites are more prevalent in server-side code (24%) than in client-side code (9%) . 31

  32. How prevalent are callbacks? Callback-accepting function definitions ● On average, 10% of all function definitions take callback arguments. ● They are more prevalent in server-side code (10%) than in • Every 10 th function definition takes a callback. client- side code (4.5%) . • Every 5 th function call-site takes a callback. Callback-accepting function callsites • Callbacks are extensively used in the server-side. ● 19% of all function callsites take callback arguments. ● Callback-accepting function call-sites are more prevalent in server-side code (24%) than in client-side code (9%) . 32

  33. Outline Motivation ✔ Methodology & Overview ✔ Characterizing Problems Anonymous Callbacks Asynchronous Callbacks Nested Callbacks Characterizing Solutions Error-first Protocol Async.js Promises Conclusion 33

  34. Outline Motivation ✔ Methodology & Overview ✔ ► Characterizing Problems Anonymous Callbacks Asynchronous Callbacks Nested Callbacks Characterizing Solutions Error-first Protocol Async.js Promises Conclusion 34

  35. JavaScript in the wild... 35

  36. Anonymous callbacks Notorious for: Difficulty to debug, maintain, test, or reuse 36

  37. Anonymous Vs. Named Callbacks If a function callsite is .. ● callback-accepting and ● has an anonymous function expression as an argument ..it is an instance of an anonymous callback. 37

Recommend


More recommend