not-so-object oriented programming
TRANSCRIPT
![Page 1: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/1.jpg)
Programowanie nie do końca obiektowe
…czyli kilka sposobów na przeżycie bez klas.
Przemek Smyrdek
![Page 2: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/2.jpg)
Jak programujemy?
Imperatywnie Deklaratywnie
Proceduralnie LogicznieObiektowo Funkcyjnie
![Page 3: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/3.jpg)
Obiektowo jest wporzo.
Klasy odwzorowują obiekty biznesowe
Działanie metod jest przewidywalne
OOP wspiera dobre praktyki
![Page 4: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/4.jpg)
To prawda, ale…
![Page 5: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/5.jpg)
![Page 6: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/6.jpg)
Ukryte wejścia / wyjścia
Efekty uboczne
![Page 7: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/7.jpg)
![Page 8: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/8.jpg)
Stan algorytmu / klasy / etc.
![Page 9: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/9.jpg)
![Page 10: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/10.jpg)
Modyfikacja (mutacja) parametrów
![Page 11: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/11.jpg)
Obiektowo jest wporzo?
Klasy odwzorowują obiekty biznesowe(a o wszystkim decyduje ich aktualny stan)
Działanie metod jest przewidywalne(ale nie ma problemu żeby wykonać coś „na boku”)
OOP wspiera dobre praktyki(ale nie zabrania złych)
![Page 12: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/12.jpg)
Funkcyjnie? No coś słyszałem, ale…
![Page 13: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/13.jpg)
No coś słyszałem, ale…
…w sumie po co, skoro C# / Ruby / Java mi wystarczy?
…w sumie po co, skoro od zawsze robiłem w jQuery i działało?
…w sumie po co, skoro nie znam a i tak mi płacą?
…w sumie po co, skoro…
![Page 14: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/14.jpg)
Problem nr 1 – system edukacji
![Page 15: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/15.jpg)
„To, że ciągle robisz coś w określony sposób
wcale nie oznacza, że jest to najlepszy sposób
na robienie tego czegoś.”
Problem nr 2 – przyzwyczajenia
![Page 16: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/16.jpg)
„JS jest złym językiem, bo nie ma klas.”
Problem nr 3 – OOP jako lek na wszystko
![Page 17: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/17.jpg)
Warto poszerzać horyzonty
![Page 18: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/18.jpg)
Co zdiagnozowaliśmy do tej pory?
efekty uboczne
globalny stan
modyfikacja parametrów
![Page 19: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/19.jpg)
A gdyby tak użyć funkcji matematycznych…
y = f(x)
![Page 20: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/20.jpg)
f(2) = 12
Przewidywalne zachowanie
Mapowanie wejścia na wyjście
Wysoki poziom abstrakcji
![Page 21: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/21.jpg)
Dziedzina
Abstrakcja
![Page 22: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/22.jpg)
12345…
357911…
f(x) = 2x + 1
![Page 23: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/23.jpg)
12345…
357911…
public int MultiplyAndAddOne(int input){
int multiplied = input * 2;int addOneResult = multiplied + 1;return addOneResult;
}
![Page 24: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/24.jpg)
12345…
357911…
public int MultiplyAndAddOne(int input){
switch(input){
case 1: return 3;case 2: return 5;case 3: return 7;…
}}
![Page 25: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/25.jpg)
Brak efektów ubocznych
(no side-effects)
Brak mutacji parametru
(immutability)
Funkcja jest bezstanowa
(stateless)
public int MultiplyAndAddOne(int input){
switch(input){
case 1: return 3;case 2: return 5;case 3: return 7;…
}}
![Page 26: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/26.jpg)
=> Przewidywalne zachowanie
=> Łatwe testowanie
=> Zmiana nawyków
public int MultiplyAndAddOne(int input){
switch(input){
case 1: return 3;case 2: return 5;case 3: return 7;…
}}
![Page 27: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/27.jpg)
map
filter
reduce
…
Funkcyjnie, czyli co zrobić, a nie jak zrobić:
![Page 28: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/28.jpg)
map
![Page 29: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/29.jpg)
filter
![Page 30: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/30.jpg)
reduce
![Page 31: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/31.jpg)
Funkcja to obiekt pierwszej klasy
![Page 32: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/32.jpg)
Przypisanie funkcji
![Page 33: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/33.jpg)
Funkcja jako parametr
![Page 34: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/34.jpg)
Funkcja zwracana z innej funkcji
![Page 35: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/35.jpg)
Języki funkcyjne
Czysto funkcyjne
Mieszane
![Page 36: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/36.jpg)
Programowanie funkcyjne x 2
![Page 37: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/37.jpg)
F#
• Microsoft, 2005
• Pozwala na tworzenie kodu w wielu paradygmatach
• Mocno typowany
• Oparty o platformę .NET
• Visual Studio
• Pipes, pattern matching, async
![Page 38: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/38.jpg)
F# - DEMO
![Page 39: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/39.jpg)
Reactive Extensions
• „Reactive Functional Programming”
• Programowanie reaktywne – wykorzystanie wzorca Obserwator
• Cross-platform (.NET, JS, Java, …)
• Kompozycja oparta o strumienie danych
• Async
![Page 40: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/40.jpg)
Rx.Observable
![Page 41: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/41.jpg)
https://github.com/Reactive-Extensions/Rx.NET
![Page 42: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/42.jpg)
https://medium.com/google-developer-experts/angular-introduction-to-reactive-extensions-rxjs-a86a7430a61f
var observer = rx.Observer.create(function onNext(result){ console.log(result);
},function onError(err){ console.log(err);
},function onCompleted(){ console.log('Completed');
});
observable.subscribe(observer);
![Page 43: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/43.jpg)
Strumień eventów
czas’click’ ’click’ ’click’’click’’click’’click’
![Page 44: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/44.jpg)
Map, reduce, filter!
![Page 45: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/45.jpg)
map
’click’ ’click’ ’click’’click’’click’’click’
1 1 11 1 1
map
![Page 46: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/46.jpg)
var onesStream = Rx.Observable.fromEvent(button, ’click’).map( () => 1 );
onesStream.subscribe(function(value) { … });
![Page 47: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/47.jpg)
Rx - DEMO
![Page 48: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/48.jpg)
https://glebbahmutov.com/draw-cycle/
![Page 49: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/49.jpg)
http://rxmarbles.com/
![Page 50: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/50.jpg)
Pułapki
Wdrożenie „czystego” FP w zespole może okazać się zbyt kosztowne
O wiele mniejsza baza zasobów w porównaniu do OOP
Problematyczna optymalizacja
![Page 51: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/51.jpg)
Dobre praktyki OOP / FP
SRP / Minimalizowanie efektów ubocznych (no side-effects)
Przewidywalne zachowanie funkcji (immutability)
Minimalizowanie globalnego stanu (stateless)
![Page 52: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/52.jpg)
„To, że ciągle robisz coś w określony sposób
wcale nie oznacza, że jest to najlepszy sposób
na robienie tego czegoś.”
![Page 53: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/53.jpg)
Credits:
http://fsharpforfunandprofit.com/posts/thinking-functionally-intro/
https://www.youtube.com/watch?v=1zj7M1LnJV4
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
http://wazniak.mimuw.edu.pl/index.php?title=Programowanie_funkcyjne
http://blog.reactandbethankful.com/posts/2015/09/15/understanding-the-functional-revolution/
http://www.mpscholten.de/javascript/2016/01/15/javascript-in-2016-functional-programming-is-getting-here-to-stay.html
http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html
https://glebbahmutov.com/draw-cycle/
http://reactivex.io/intro.html
http://fsharp.org/about/index.html
http://www.scs.stanford.edu/16wi-cs240h/slides/fb-slides.html
https://brianmckenna.org/blog/howtostopfp
and more…
![Page 54: Not-So-Object Oriented Programming](https://reader031.vdocuments.mx/reader031/viewer/2022022200/58ab4df41a28ab61758b6693/html5/thumbnails/54.jpg)
Dzięki :)