use promise, future and some functional programing stuff without being a math nerd. - #devoxx maroc...
TRANSCRIPT
Use Promise, Future and some functional programing stuff without being a math nerd.
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 designKeep your apps online. made with node.js, scala, java, ruby, php, python, go
And learn a lot of things about your code, apps, and good/bad design
Im 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 shouldnt drive your code architecture, theorically
So, we have a problem : how to use a value we dont have yet, and use it in the future?
Change your mindhow do you interact with data now?
example
You want to return something !
you mix I/O handling with your business processesThe other problem :
Split
Change your mindhow can we do it?
Work on future data you have promised to your code ;-)
Examplesimple promise map
Examplesimple promise map
More important : you can return a data you dont 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 ?
Weve 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 : its the ability to use algebraic structures and math tools in your codeSo you can reuse it where you want
Made by the awesome Brian McKenna - @puffnfreshFunctional Javascript : fantasy land specification and code
https://github.com/petkaantonov/bluebirdBluebird
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 modelOption
Example
Example
Act optional the time you need, then decide
Code is more clean & you avoid NPE
Made by the awesome Brian McKenna - @puffnfreshFunctional 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 errorsCode clarityBe predicable
Loops ?
Classic iteration
Classic iteration
Classic iteration
Much more readable !
Using underscore.js and lodash.js
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
Im on twitter : @waxzceThank you !Any questions ?