Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
USE PROMISES,
FUTURES AND SOME
FUNCTIONAL
PROGRAMING STUFF
WITHOUT BEING A
MATH NERD.
@WAXZCE
QUENTIN ADAM AT
Quentin ADAM from the Clever Cloud
@waxzce on twitter – github- soundcloud – instagram ….
WHO AM I ?
MY DAY TO DAY WORK :
CLEVER CLOUD, MAKE YOUR
APP RUN ALL THE TIME
And learn a lot of things about your code, apps, and good/bad design…
KEEP YOUR APPS ONLINE. MADE WITH
NODE.JS, SCALA, JAV...
AND LEARN A LOT OF THINGS ABOUT
YOUR CODE, APPS, AND GOOD/BAD
DESIGN…
I’M NOT A MATH NERD
NODE.JS MAIN PROBLEM
SO, TRY TO KEEP IT ALIVE AND
MINIMIZE ERROR EXPOSURE
ON THE OTHER HAND:
THE CALLBACK SOUP
EXAMPLE :
DATABASE CONNECTION
REALLY ?
ALL DB INTERACTION IN ONE
CALLBACK ?
REALLY ?
UNCATCHED EXCEPTIONS BUBBLE TO
THE TOP AND KILL THE APP ?
REALLY ?
IF/ELSE/UNDEFINED ERROR
TREATMENT ?
DB DRIVER
SHOULDN’T
DRIVE YOUR
CODE
ARCHITECTURE,
THEORICALLY
SO, WE HAVE A PROBLEM : HOW TO USE
A VALUE WE DON’T HAVE YET, AND USE
IT IN THE FUTURE?
CHANGE YOUR MIND
HOW DO YOU INTERACT WITH
DATA NOW?
• Wait for it
• Possibly
blocking
i/o
Get data
from DB
and/or
WS
• Act...
EXAMPLE
YOU WANT TO RETURN SOMETHING !
YOU MIX I/O
HANDLING WITH
YOUR BUSINESS
PROCESSES
THE OTHER PROBLEM :
SPLIT
Business
process and
valued
computing
I/O management
and other
“computing stuff”
CHANGE YOUR MIND
HOW CAN WE DO IT?
• Give all the
“recipes” about
how to deal
with this data
• You only
describe how
to ma...
WORK ON
FUTURE DATA
YOU HAVE
PROMISED TO
YOUR CODE
;-)
EXAMPLE
SIMPLE PROMISE MAP
EXAMPLE
SIMPLE PROMISE MAP
MORE IMPORTANT : YOU CAN
RETURN A DATA YOU DON’T
HAVE AT THE MOMENT
NO MORE DEALING WITH BLOCKING I/O
IN YOUR BUSINESS CODE
DEALING WITH
MULTIPLE FUTURE DATA
And stop using boolean counter for event « ready »….
GET ALL YOU NEED TO WORK
REAL WORLD EXAMPLE
BUILT IN ERROR
MANAGEMENT
SIMPLE WAY : YOU CAN
RETURN SOMETING :-)
PROMISES
WILL MANAGE
TEMPORALITY
PROBLEMS
AND I/O IN A
SIMPLE WAY
NOT TOO COMPLICATED ?
WE’VE JUST DEALT
WITH FUNCTORS,
MONADS,
SEMIGROUPS… ALL
OF THESE
ALGEBRAIC
STRUCTURES
MATH WORDS, NOT SO COMPLICATED,
JUST MADE TO SHARE CONCEPTS
BETWEEN PEOPLE :-)
FUNCTIONAL
PROGRAMING IS NOT
A LANGUAGE : IT’S
THE ABILITY TO USE
ALGEBRAIC
STRUCTURES AND
MATH TOOLS IN YOUR
CODE
SO YOU ...
Made by the awesome Brian McKenna - @puffnfresh
FUNCTIONAL JAVASCRIPT : FANTASY
LAND SPECIFICATION AND CODE
https://github.com/petkaantonov/bluebird
BLUEBIRD
TRY IT YOU WILL
BUT THERE
IS MORE
DATA STRUCTURES CAN DRIVE
YOUR CODE TO AVOID RUNTIME
PROBLEMS
NULL POINTER EXCEPTION
PUT LOT OF IF/ELSE
IN YOUR CODE ?
OPTIONAL
TYPE
MAYBE YOU CAN USE
SAFE STORAGE BOX
ALGEBRA DATA MODEL
Some(DATA)
None
Option<A>
EXAMPLE
EXAMPLE
ACT OPTIONAL THE TIME YOU NEED,
THEN DECIDE
CODE IS
MORE
CLEAN &
YOU AVOID
NPE
Made by the awesome Brian McKenna - @puffnfresh
FUNCTIONAL JAVASCRIPT : FANTASY
LAND SPECIFICATION AND CODE
French article : http://www.touilleur-express.fr/2014/11/07/optional-en-java-8/
JAVA 8 OPTIONAL TYPE !
To understand Functional pattern value
OR TRY SCALA
FUNCTIONAL GOAL :
• Avoid runtime errors
• Code clarity
• Be predicable
LOOPS ?
CLASSIC ITERATION
CLASSIC ITERATION
CLASSIC ITERATION
MUCH MORE READABLE !
USING UNDERSCORE.JS
AND LODASH.JS
access to
new features
backward
compatibility
developer
hapiness
MODERN JAVASCRIPT
IMPLEMENTATION
FUNCTIONAL :
APPLY
FUNCTION ON
DATA
SIMPLE
YOU CAN USE
IT
EVERYWHERE
BE A BADASS !
I give you a www.clever-
cloud.com gift :
devoxxMA15
I’m on twitter : @waxzce
THANK YOU !
ANY QUESTIONS ?
Use Promise, Future and some functional programing stuff without being a math nerd.  - #devoxx Maroc 2015
Upcoming SlideShare
Loading in …5
×

Use Promise, Future and some functional programing stuff without being a math nerd. - #devoxx Maroc 2015

Javascript is awesome, and asynchronous. But some libraries are made by egocentric people : how to contain all DB interaction to the connection callback? Does the Callback soup a fatality? This talk help you to use data structure to keep safe your code. Some great ideas are comming from the functional programing world. How to use it with javascript browser and/or node.js? Is it really complicated?

I write this talk because I use node.js a lot and write a lot of fix in my code to abstract database driver (couchbase, mongo, postgre...). I found an effective way to solve problems using functional programing data structure. But this talk is not for nerd or claiming functional programing superiority : some tools are cool, make your code cleanest and modular, so use it !

Use Promise, Future and some functional programing stuff without being a math nerd. - #devoxx Maroc 2015

  1. 1. USE PROMISES, FUTURES AND SOME FUNCTIONAL PROGRAMING STUFF WITHOUT BEING A MATH NERD. @WAXZCE QUENTIN ADAM AT
  2. 2. Quentin ADAM from the Clever Cloud @waxzce on twitter – github- soundcloud – instagram …. WHO AM I ?
  3. 3. MY DAY TO DAY WORK : CLEVER CLOUD, MAKE YOUR APP RUN ALL THE TIME
  4. 4. And learn a lot of things about your code, apps, and good/bad design… KEEP YOUR APPS ONLINE. MADE WITH NODE.JS, SCALA, JAVA, RUBY, PHP, PYTHON, GO…
  5. 5. AND LEARN A LOT OF THINGS ABOUT YOUR CODE, APPS, AND GOOD/BAD DESIGN…
  6. 6. I’M NOT A MATH NERD
  7. 7. NODE.JS MAIN PROBLEM
  8. 8. SO, TRY TO KEEP IT ALIVE AND MINIMIZE ERROR EXPOSURE
  9. 9. ON THE OTHER HAND: THE CALLBACK SOUP
  10. 10. EXAMPLE : DATABASE CONNECTION
  11. 11. REALLY ? ALL DB INTERACTION IN ONE CALLBACK ?
  12. 12. REALLY ? UNCATCHED EXCEPTIONS BUBBLE TO THE TOP AND KILL THE APP ?
  13. 13. REALLY ? IF/ELSE/UNDEFINED ERROR TREATMENT ?
  14. 14. DB DRIVER SHOULDN’T DRIVE YOUR CODE ARCHITECTURE, THEORICALLY
  15. 15. SO, WE HAVE A PROBLEM : HOW TO USE A VALUE WE DON’T HAVE YET, AND USE IT IN THE FUTURE?
  16. 16. CHANGE YOUR MIND HOW DO YOU INTERACT WITH DATA NOW? • Wait for it • Possibly blocking i/o Get data from DB and/or WS • Act on it • Data treatment Data is here
  17. 17. EXAMPLE
  18. 18. YOU WANT TO RETURN SOMETHING !
  19. 19. YOU MIX I/O HANDLING WITH YOUR BUSINESS PROCESSES THE OTHER PROBLEM :
  20. 20. SPLIT Business process and valued computing I/O management and other “computing stuff”
  21. 21. CHANGE YOUR MIND HOW CAN WE DO IT? • Give all the “recipes” about how to deal with this data • You only describe how to manage data you get Get data from DB and/or WS That’s all
  22. 22. WORK ON FUTURE DATA YOU HAVE PROMISED TO YOUR CODE ;-)
  23. 23. EXAMPLE SIMPLE PROMISE MAP
  24. 24. EXAMPLE SIMPLE PROMISE MAP
  25. 25. MORE IMPORTANT : YOU CAN RETURN A DATA YOU DON’T HAVE AT THE MOMENT
  26. 26. NO MORE DEALING WITH BLOCKING I/O IN YOUR BUSINESS CODE
  27. 27. DEALING WITH MULTIPLE FUTURE DATA
  28. 28. And stop using boolean counter for event « ready »…. GET ALL YOU NEED TO WORK
  29. 29. REAL WORLD EXAMPLE
  30. 30. BUILT IN ERROR MANAGEMENT
  31. 31. SIMPLE WAY : YOU CAN RETURN SOMETING :-)
  32. 32. PROMISES WILL MANAGE TEMPORALITY PROBLEMS AND I/O IN A SIMPLE WAY
  33. 33. NOT TOO COMPLICATED ?
  34. 34. WE’VE JUST DEALT WITH FUNCTORS, MONADS, SEMIGROUPS… ALL OF THESE ALGEBRAIC STRUCTURES
  35. 35. MATH WORDS, NOT SO COMPLICATED, JUST MADE TO SHARE CONCEPTS BETWEEN PEOPLE :-)
  36. 36. FUNCTIONAL PROGRAMING IS NOT A LANGUAGE : IT’S THE ABILITY TO USE ALGEBRAIC STRUCTURES AND MATH TOOLS IN YOUR CODE SO YOU CAN REUSE IT WHERE YOU WANT…
  37. 37. Made by the awesome Brian McKenna - @puffnfresh FUNCTIONAL JAVASCRIPT : FANTASY LAND SPECIFICATION AND CODE
  38. 38. https://github.com/petkaantonov/bluebird BLUEBIRD
  39. 39. TRY IT YOU WILL
  40. 40. BUT THERE IS MORE
  41. 41. DATA STRUCTURES CAN DRIVE YOUR CODE TO AVOID RUNTIME PROBLEMS
  42. 42. NULL POINTER EXCEPTION
  43. 43. PUT LOT OF IF/ELSE IN YOUR CODE ?
  44. 44. OPTIONAL TYPE MAYBE YOU CAN USE
  45. 45. SAFE STORAGE BOX
  46. 46. ALGEBRA DATA MODEL Some(DATA) None Option<A>
  47. 47. EXAMPLE
  48. 48. EXAMPLE
  49. 49. ACT OPTIONAL THE TIME YOU NEED, THEN DECIDE
  50. 50. CODE IS MORE CLEAN & YOU AVOID NPE
  51. 51. Made by the awesome Brian McKenna - @puffnfresh FUNCTIONAL JAVASCRIPT : FANTASY LAND SPECIFICATION AND CODE
  52. 52. French article : http://www.touilleur-express.fr/2014/11/07/optional-en-java-8/ JAVA 8 OPTIONAL TYPE !
  53. 53. To understand Functional pattern value OR TRY SCALA
  54. 54. FUNCTIONAL GOAL : • Avoid runtime errors • Code clarity • Be predicable
  55. 55. LOOPS ?
  56. 56. CLASSIC ITERATION
  57. 57. CLASSIC ITERATION
  58. 58. CLASSIC ITERATION
  59. 59. MUCH MORE READABLE !
  60. 60. USING UNDERSCORE.JS AND LODASH.JS access to new features backward compatibility developer hapiness
  61. 61. MODERN JAVASCRIPT IMPLEMENTATION
  62. 62. FUNCTIONAL : APPLY FUNCTION ON DATA
  63. 63. SIMPLE
  64. 64. YOU CAN USE IT EVERYWHERE
  65. 65. BE A BADASS !
  66. 66. I give you a www.clever- cloud.com gift : devoxxMA15 I’m on twitter : @waxzce THANK YOU ! ANY QUESTIONS ?

×