Aftermath of functional programming. The good parts
-
Upload
guillermo-gutierrez -
Category
Software
-
view
127 -
download
1
Transcript of Aftermath of functional programming. The good parts
aftermath of functional programming
the good parts
@ggalmazor @buntplanet
disclaimer
functional programming
object orientation
“FP != OO”
mutation
why bother
equality rules
Stuff a = new Stuff();Stuff b = a.changeSomething(); if (a.equals(b)) System.out.println("WTF");
output argsStuff a = new Stuff(); // a.something holds 42 doStuffWith(a); // a.something holds 33 // WTF
entity != value
surprisesa.setSuch();a.setWow();a.setVery();a.setCoupling();
concurrency
control
how to avoid it
change your design
isolate mutation
copy on write
tips
ValueObjectclass Stuff { private final Integer aNumber; private final String aString; Stuff(Integer aNumber, String aString) { this.aNumber = aNumber; this.aString = aString; } … }
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
ValueObjectclass Stuff { …
@Override public int hashCode() { return Objects.hash(aNumber, aString); } … }
factories
functional data structures
side effects
why bother
accidental complexity
coupling
cqrs
object serialization in a distributed context
final AtomicInteger counter = new AtomicInteger(0);rdd.map(stuff -> { int i = counter(); return OtheStuff(i, stuff);});
tips
think in pure functions
isolate and identify side effects
functional data structures
tools
option
nulls
monad
antipatterns
Optional.ofNullable(maybeNull).get();
antipatternsOptional<Object> o = Optional.ofNullable(maybeNull); if (o.isPresent()) doStuffWith(o.get());
antipatrones
Optional<List<Object>> foo
antipatrones
Map<String,Optional<Object>> foo
tips
tuple
design exploration
tips
try
exceptions
when i use try
when i use try
Try.of(someComputation)
Try.of(someComputation) .onFailure(logger::error) .get()
when i use try
when i use tryTry.of(someComputation) .flatMap(someOtherComputation) .onFailure(logger::error) .get()
when i use tryTry.run(someVoidComputation) .anThen(someVoidComputation) .onFailure(logger::error) .get()
when i use tryTry.of(someComputation) .orElse(42)
antipatterns
antipatternsTry.run(()-> { int a = somethingOverHere(); int b = somethingOverThere(a); doTheGangnamStyle(a,b); }).onFailure(t-> { logger.error(“Everything’s f*cked!!!”, t); }).get();
antipatternsTry.of(thisWillReturnNull).toOption()
antipatronesTry.of(someVoidComputation) .map(thisWillGetDirty)
functional data structures
why bother
know your tools
filter, map, fold
impact in your design
tips
small blocks
don’t go crazy
really. don’t go crazy
types
thanks!