1 / 80

functional principles for object-oriented development

functional principles for object-oriented development. @jessitron. Imperative. Procedural. Functional. Object-Oriented. Logic. Aspect-Oriented. Data In, Data Out. Immutability. Verbs Are People Too. Declarative Style. Specific Typing. Lazy Evaluation. What is it?. Principle.

Download Presentation

functional principles for object-oriented development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. functional principles for object-oriented development • @jessitron

  2. Imperative Procedural Functional Object-Oriented Logic Aspect-Oriented

  3. Data In, Data Out Immutability Verbs Are People Too Declarative Style Specific Typing Lazy Evaluation

  4. What is it? Principle Java / C# What is it? Scala / F# What’s the point? We already do it What is it?

  5. Data In, Data Out

  6. access global state modify input change the world

  7. output input Data In -> ? -> Data out

  8. Testable Easier to understand

  9. the flow of data

  10. List<Deposit> List<Donation> List<Match<Deposit,Donation>> List<Donation>

  11. Problem easier, because we know each step of the way of data.

  12. public string CreateAccount(UserDbService db, String email, String password) { … }

  13. String password; String email; private boolean invalidPassword() { return !password.contains(email); }

  14. private boolean invalidPassword(String password, String email) { return !password.contains(email); }

  15. Immutability

  16. modify anything

  17. Concurrency fewer possibilities

  18. NPE

  19. c c

  20. String Effective Java Effective C#

  21. Scala val qty = 4 // immutable var n = 2 // mutable

  22. F# let qty = 4 // immutable let mutable n = 2 // mutable n = 4 // false n <- 4 // destructive update

  23. C#: easy public struct Address { private readonly string city; public Address(string city) : this() { this.city = city; } public string City { get { return city; } }

  24. Java: defensive copy private final ImmutableList<Phone> phones; public Customer (Iterable<Phone> phones) { this.phones = ImmutableList.copyOf(phones); }

  25. Java: copy on mod public Customer addPhone(Phone newPhone) { Iterable<Phone> morePhones = ImmutableList.builder() .addAll(phones) .add(newPhone).build(); return new Customer(morePhones); }

  26. fruit.add(tomato) fruit

  27. F#: copy on mod member this.AddPhone (newPhone : Phone) { new Customer(newPhone :: phones) }

  28. C#: shallow copy public Customer AddPhone(Phone newPhone) { IEnumerable<Phone> morePhones = // create list return new Customer(morePhones, name, address, birthday, cousins); }

  29. Thing doStuff() Option<T> NPE SomeThing doStuff() {…} Some<T> NullThing doStuff() {} None this talk is brought to you by… the Option type!

  30. FSharpOption<T> Optional<T> … because null is not a valid object reference.

  31. Verbs Are People Too

  32. Imperative Procedural != Functional Object-Oriented Logic Aspect-Oriented

  33. release ( ) OnClick() Command Strategy

  34. Java class CostInflation implements FunctionOverTime { public float valueAt(int t) { return // some calculated value; } }

  35. Scala val costInflation = Variable ( “Cost Inflation”, t => Math.pow(1.15,t)) val seasonalVolume = Array(0.95,0.99,1.01,…) val seasonalAdjustment = Variable(“Season”, t => seasonalVolume(t))

  36. C# private readonly Func<int, double> calc; public Func<int, double> ValueAt { get { return calc; } }

  37. C# var inflation = new Variable(“Inflation”, t => Math.Pow(1.15,t)); inflation.ValueAt(3);

  38. Java Variable inflation = new Variable (“Inflation”, new Function<Integer, Double>() { @Override public Double apply(Integer input) { return Math.pow(1.15, input); } });

  39. Java 8 Variable inflation = new Variable (“Inflation”, t => Math.pow(1.15, t) );

  40. Declarative Style

  41. say what you’re doing, not how you’re doing it

  42. Select ROLE_NAME, UPDATE_DATE From USER_ROLES Where USER_ID = :userId

  43. readable code smaller pieces

  44. familiar !=readable

More Related