+ All Categories
Home > Documents > Functional principles for object-oriented development @jessitron.

Functional principles for object-oriented development @jessitron.

Date post: 26-Dec-2015
Category:
Upload: ronald-kennedy
View: 226 times
Download: 3 times
Share this document with a friend
80
functional principles for object-oriented development @jessitron
Transcript
Page 1: Functional principles for object-oriented development @jessitron.

functional principles for

object-oriented development

@jessitron

Page 2: Functional principles for object-oriented development @jessitron.
Page 3: Functional principles for object-oriented development @jessitron.
Page 4: Functional principles for object-oriented development @jessitron.

Imperative

Procedural

Object-Oriented Functional

Aspect-Oriented Logic

Page 5: Functional principles for object-oriented development @jessitron.
Page 6: Functional principles for object-oriented development @jessitron.
Page 7: Functional principles for object-oriented development @jessitron.

Data In, Data Out

Immutability

Verbs Are People Too

Declarative Style

Specific Typing

Lazy Evaluation

Page 8: Functional principles for object-oriented development @jessitron.

What is it?What is it?

PrinciplePrinciple

What is it?What is it?

We already do itWe already do it

What’s the point?What’s the point?

What is it?What is it?

Scala / F#Scala / F#

Java / C#Java / C#

Page 9: Functional principles for object-oriented development @jessitron.

Data In, Data Out

Page 10: Functional principles for object-oriented development @jessitron.

access global state

modify input

change the world

Page 11: Functional principles for object-oriented development @jessitron.

Data In -> ? -> Data out

inputoutput

Page 12: Functional principles for object-oriented development @jessitron.

Testable

Easier to understand

Page 13: Functional principles for object-oriented development @jessitron.

the flow of datathe flow of data

Page 14: Functional principles for object-oriented development @jessitron.
Page 15: Functional principles for object-oriented development @jessitron.

List<Deposit> List<Donation>

List<Match<Deposit,Donation>>

List<Donation>

Page 16: Functional principles for object-oriented development @jessitron.

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

Page 17: Functional principles for object-oriented development @jessitron.

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

Page 18: Functional principles for object-oriented development @jessitron.

String password;String email;

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

Page 19: Functional principles for object-oriented development @jessitron.

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

Page 20: Functional principles for object-oriented development @jessitron.

Immutability

Page 21: Functional principles for object-oriented development @jessitron.

modify anything

Page 22: Functional principles for object-oriented development @jessitron.

Concurrency

fewer possibilities

Page 23: Functional principles for object-oriented development @jessitron.

NPENPE

Page 24: Functional principles for object-oriented development @jessitron.

c c

Page 25: Functional principles for object-oriented development @jessitron.

String

Effective JavaEffective C#

Page 26: Functional principles for object-oriented development @jessitron.

ScalaScala

val qty = 4 // immutable

var n = 2 // mutable

Page 27: Functional principles for object-oriented development @jessitron.

let qty = 4 // immutable

let mutable n = 2 // mutable

n = 4 // false

n <- 4 // destructive update

F#F#

Page 28: Functional principles for object-oriented development @jessitron.

C#: easyC#: easy

public struct Address { private readonly string city;

public Address(string city) : this() { this.city = city; }

public string City { get { return city; } }

Page 29: Functional principles for object-oriented development @jessitron.

Java: defensive copyJava: defensive copy

private final ImmutableList<Phone> phones;

public Customer (Iterable<Phone> phones) { this.phones = ImmutableList.copyOf(phones);}

Page 30: Functional principles for object-oriented development @jessitron.

Java: copy on modJava: copy on mod

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

Page 31: Functional principles for object-oriented development @jessitron.

fruit

fruit.add(tomato)

Page 32: Functional principles for object-oriented development @jessitron.

F#: copy on modF#: copy on mod

member this.AddPhone (newPhone : Phone) { new Customer(newPhone :: phones)}

Page 33: Functional principles for object-oriented development @jessitron.

C#: shallow copyC#: shallow copy

public Customer AddPhone(Phone newPhone) { IEnumerable<Phone> morePhones = // create list return new Customer(morePhones, name, address, birthday, cousins);}

Page 34: Functional principles for object-oriented development @jessitron.

this talk is brought to you by… the Option type!

NPENPE Thing

doStuff()

NullThing

doStuff() {}

SomeThing

doStuff() {…}

Option<T>Option<T>

NoneNoneSome<T>Some<T>

Page 35: Functional principles for object-oriented development @jessitron.

… because null is not a valid object reference.

Optional<T>

FSharpOption<T>

Page 36: Functional principles for object-oriented development @jessitron.

Verbs Are People Too

Page 37: Functional principles for object-oriented development @jessitron.
Page 38: Functional principles for object-oriented development @jessitron.

Imperative

Procedural

Object-Oriented

Functional

Aspect-Oriented Logic

!=

Page 39: Functional principles for object-oriented development @jessitron.

Strategy

CommandOnClick()

release ( )

Page 40: Functional principles for object-oriented development @jessitron.

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

JavaJava

Page 41: Functional principles for object-oriented development @jessitron.

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))

ScalaScala

Page 42: Functional principles for object-oriented development @jessitron.

C#C#

private readonly Func<int, double> calc;

public Func<int, double> ValueAt{ get { return calc; }}

Page 43: Functional principles for object-oriented development @jessitron.

C#C#

var inflation = new Variable(“Inflation”, t => Math.Pow(1.15,t));

inflation.ValueAt(3);

Page 44: Functional principles for object-oriented development @jessitron.

JavaJava

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

Page 45: Functional principles for object-oriented development @jessitron.

Java 8Java 8

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

Page 46: Functional principles for object-oriented development @jessitron.

Declarative Style

Page 47: Functional principles for object-oriented development @jessitron.

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

Page 48: Functional principles for object-oriented development @jessitron.

Select ROLE_NAME, UPDATE_DATEFrom USER_ROLESWhere USER_ID = :userId

Page 49: Functional principles for object-oriented development @jessitron.

smaller pieces

readable code

Page 50: Functional principles for object-oriented development @jessitron.

familiar != readable

familiar != readable

Page 51: Functional principles for object-oriented development @jessitron.

JavaJava

public List<String> findBugReports(List<String> lines){ List<String> output = new LinkedList(); for(String s : lines) { if(s.startsWith(“BUG”)) { output.add(s); } } return output;}

Page 52: Functional principles for object-oriented development @jessitron.

ScalaScala

lines.filter( _.startsWith(“BUG”))

lines.par.filter( _.startsWith(“BUG”))

Page 53: Functional principles for object-oriented development @jessitron.

JavaJava

final Predicate<String> startsWithBug = new Predicate<String>() { public boolean apply(String s) { return s.startsWith("BUG"); } };

filter(list, startsWithBug);

Page 54: Functional principles for object-oriented development @jessitron.

C#C#

lines.Where(s => s.StartsWith(“BUG”)).ToList

Page 55: Functional principles for object-oriented development @jessitron.

this talk is brought to you by… the Tuple type!

function

new Tuple<string,int>(“You win!”, 1000000)

Page 56: Functional principles for object-oriented development @jessitron.

When one return value is not enough!

Tuple<T1,T2,…>

Page 57: Functional principles for object-oriented development @jessitron.

Specific Typing

Page 58: Functional principles for object-oriented development @jessitron.

expressiveness

catch errors early

Page 59: Functional principles for object-oriented development @jessitron.
Page 60: Functional principles for object-oriented development @jessitron.
Page 61: Functional principles for object-oriented development @jessitron.

The beginning of wisdom is to call things by their right names.

Page 62: Functional principles for object-oriented development @jessitron.

F#F#

[<Measure>] type cents

let price = 599<cents>

Page 63: Functional principles for object-oriented development @jessitron.

ScalaScala

type FirstName = String // type alias

Customer(name : FirstName, home : EmailAddress)

Page 64: Functional principles for object-oriented development @jessitron.

ScalaScala

case class FirstName(value : String)

let friend = FirstName(“Ivana”);

Page 65: Functional principles for object-oriented development @jessitron.

JavaJava

public User(FirstName name, EmailAddress login)

public class FirstName { public final String stringValue;

public FirstName(final String value) { this.stringValue = value; }

public String toString() {...} public boolean equals() {...} public int hashCode() {...}}

Page 66: Functional principles for object-oriented development @jessitron.

State everything you

need

State only what you

need

Page 68: Functional principles for object-oriented development @jessitron.

C#: weakC#: weak

public boolean Valid(Customer cust) { EmailAddress email = cust.EmailAddress; // exercise business logic return true; }

Page 69: Functional principles for object-oriented development @jessitron.

C#: weakC#: weak

public boolean Valid(IHasEmailAddress anything) { EmailAddress email = anything.EmailAddress; // exercise business logic return true; }

interface IHasEmailAddress { string EmailAddress {get; } }

Page 70: Functional principles for object-oriented development @jessitron.

Lazy Evaluation

Page 71: Functional principles for object-oriented development @jessitron.

delay evaluation until the last responsible moment

Page 72: Functional principles for object-oriented development @jessitron.

save work

separate “what to do” from “when to stop”

Page 73: Functional principles for object-oriented development @jessitron.

int bugCount = 0;String nextLine = file.readLine();while (bugCount < 40) { if (nextLine.startsWith("BUG")) { String[] words = nextLine.split(" "); report("Saw "+words[0]+" on "+words[1]); bugCount++; } waitUntilFileHasMoreData(file); nextLine = file.readLine();}

JavaJava

Page 74: Functional principles for object-oriented development @jessitron.

for (String s : FluentIterable.of(new RandomFileIterable(br)) .filter(STARTS_WITH_BUG_PREDICATE)

.transform(TRANSFORM_BUG_FUNCTION)

.limit(40)

.asImmutableList()) { report(s); }

JavaJava

Iterable: laziness is a virtueIterable: laziness is a virtue

Page 75: Functional principles for object-oriented development @jessitron.

C#C#

IEnumerable<string> ReadLines(StreamReader r) { while (true) { WaitUntilFileHasMoreData(r); yield return r.ReadLine(); }}

Page 76: Functional principles for object-oriented development @jessitron.

Data In, Data Out

Immutability

Verbs Are People Too

Declarative Style

Specific Typing

Lazy Evaluation

Page 77: Functional principles for object-oriented development @jessitron.

I promise not to exclude from consideration any idea

based on its source, but to consider ideas across schools and heritages

in order to find the ones that best suit the current situation.

Page 78: Functional principles for object-oriented development @jessitron.
Page 79: Functional principles for object-oriented development @jessitron.
Page 80: Functional principles for object-oriented development @jessitron.

Man, the living creature, the creating individual, is always more important than any established style or

system. – Bruce Lee

@jessitron@jessitron

Jessica Kerrblog.jessitron.com

github.com/jessitron/fp4ood


Recommended