Aftermath of functional programming. The good parts

68
aftermath of functional programming the good parts @ggalmazor @buntplanet

Transcript of Aftermath of functional programming. The good parts

Page 1: Aftermath of functional programming. The good parts

aftermath of functional programming

the good parts

@ggalmazor @buntplanet

Page 2: Aftermath of functional programming. The good parts

disclaimer

Page 3: Aftermath of functional programming. The good parts

functional programming

Page 4: Aftermath of functional programming. The good parts

object orientation

Page 5: Aftermath of functional programming. The good parts

“FP != OO”

Page 6: Aftermath of functional programming. The good parts

mutation

Page 7: Aftermath of functional programming. The good parts

why bother

Page 8: Aftermath of functional programming. The good parts

equality rules

Stuff a = new Stuff();Stuff b = a.changeSomething(); if (a.equals(b)) System.out.println("WTF");

Page 9: Aftermath of functional programming. The good parts

output argsStuff a = new Stuff(); // a.something holds 42 doStuffWith(a); // a.something holds 33 // WTF

Page 10: Aftermath of functional programming. The good parts

entity != value

Page 11: Aftermath of functional programming. The good parts

surprisesa.setSuch();a.setWow();a.setVery();a.setCoupling();

Page 12: Aftermath of functional programming. The good parts

concurrency

Page 13: Aftermath of functional programming. The good parts

control

Page 14: Aftermath of functional programming. The good parts

how to avoid it

Page 15: Aftermath of functional programming. The good parts

change your design

Page 16: Aftermath of functional programming. The good parts

isolate mutation

Page 17: Aftermath of functional programming. The good parts

copy on write

Page 18: Aftermath of functional programming. The good parts

tips

Page 19: Aftermath of functional programming. The good parts

ValueObjectclass Stuff { private final Integer aNumber; private final String aString; Stuff(Integer aNumber, String aString) { this.aNumber = aNumber; this.aString = aString; } … }

Page 20: Aftermath of functional programming. The good parts

class Stuff { … @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Stuff stuff = (Stuff) o; return Objects.equals(aNumber, stuff.aNumber) && Objects.equals(aString, stuff.aString); } … }

ValueObject

Page 21: Aftermath of functional programming. The good parts

ValueObjectclass Stuff { …

@Override public int hashCode() { return Objects.hash(aNumber, aString); } … }

Page 22: Aftermath of functional programming. The good parts

factories

Page 23: Aftermath of functional programming. The good parts

functional data structures

Page 24: Aftermath of functional programming. The good parts

side effects

Page 25: Aftermath of functional programming. The good parts

why bother

Page 26: Aftermath of functional programming. The good parts

accidental complexity

Page 27: Aftermath of functional programming. The good parts

coupling

Page 28: Aftermath of functional programming. The good parts

cqrs

Page 29: Aftermath of functional programming. The good parts

object serialization in a distributed context

final AtomicInteger counter = new AtomicInteger(0);rdd.map(stuff -> { int i = counter(); return OtheStuff(i, stuff);});

Page 30: Aftermath of functional programming. The good parts

tips

Page 31: Aftermath of functional programming. The good parts

think in pure functions

Page 32: Aftermath of functional programming. The good parts

isolate and identify side effects

Page 33: Aftermath of functional programming. The good parts

functional data structures

Page 34: Aftermath of functional programming. The good parts

tools

Page 35: Aftermath of functional programming. The good parts

option

Page 36: Aftermath of functional programming. The good parts

nulls

Page 37: Aftermath of functional programming. The good parts

monad

Page 38: Aftermath of functional programming. The good parts

antipatterns

Optional.ofNullable(maybeNull).get();

Page 39: Aftermath of functional programming. The good parts

antipatternsOptional<Object> o = Optional.ofNullable(maybeNull); if (o.isPresent()) doStuffWith(o.get());

Page 40: Aftermath of functional programming. The good parts

antipatrones

Optional<List<Object>> foo

Page 41: Aftermath of functional programming. The good parts

antipatrones

Map<String,Optional<Object>> foo

Page 42: Aftermath of functional programming. The good parts

tips

Page 43: Aftermath of functional programming. The good parts

tuple

Page 44: Aftermath of functional programming. The good parts

design exploration

Page 45: Aftermath of functional programming. The good parts

tips

Page 46: Aftermath of functional programming. The good parts

try

Page 47: Aftermath of functional programming. The good parts

exceptions

Page 48: Aftermath of functional programming. The good parts

when i use try

Page 49: Aftermath of functional programming. The good parts

when i use try

Try.of(someComputation)

Page 50: Aftermath of functional programming. The good parts

Try.of(someComputation) .onFailure(logger::error) .get()

when i use try

Page 51: Aftermath of functional programming. The good parts

when i use tryTry.of(someComputation) .flatMap(someOtherComputation) .onFailure(logger::error) .get()

Page 52: Aftermath of functional programming. The good parts

when i use tryTry.run(someVoidComputation) .anThen(someVoidComputation) .onFailure(logger::error) .get()

Page 53: Aftermath of functional programming. The good parts

when i use tryTry.of(someComputation) .orElse(42)

Page 54: Aftermath of functional programming. The good parts

antipatterns

Page 55: Aftermath of functional programming. The good parts

antipatternsTry.run(()-> { int a = somethingOverHere(); int b = somethingOverThere(a); doTheGangnamStyle(a,b); }).onFailure(t-> { logger.error(“Everything’s f*cked!!!”, t); }).get();

Page 56: Aftermath of functional programming. The good parts

antipatternsTry.of(thisWillReturnNull).toOption()

Page 57: Aftermath of functional programming. The good parts

antipatronesTry.of(someVoidComputation) .map(thisWillGetDirty)

Page 58: Aftermath of functional programming. The good parts

functional data structures

Page 59: Aftermath of functional programming. The good parts

why bother

Page 60: Aftermath of functional programming. The good parts

know your tools

Page 61: Aftermath of functional programming. The good parts

filter, map, fold

Page 62: Aftermath of functional programming. The good parts

impact in your design

Page 63: Aftermath of functional programming. The good parts

tips

Page 64: Aftermath of functional programming. The good parts

small blocks

Page 65: Aftermath of functional programming. The good parts

don’t go crazy

Page 66: Aftermath of functional programming. The good parts

really. don’t go crazy

Page 67: Aftermath of functional programming. The good parts

types

Page 68: Aftermath of functional programming. The good parts

thanks!