cómo java afecta nuestros diseños

70
agile software development & services La Influencia de Java en Nuestra Manera de Pensar Hernán Wilkinson Twitter: @HernanWilkinson Blog: objectmodels.blogspot.com www.10pines.com

Upload: hernan-wilkinson

Post on 02-Jul-2015

321 views

Category:

Software


2 download

DESCRIPTION

Presentacion de la charla de JavaConf 2014

TRANSCRIPT

Page 1: Cómo Java afecta nuestros Diseños

agile software development & services

La Influencia de Java en

Nuestra Manera de Pensar

Hernán Wilkinson

Twitter: @HernanWilkinson

Blog: objectmodels.blogspot.com

www.10pines.com

Page 2: Cómo Java afecta nuestros Diseños

The world “we live” in … ?

Page 3: Cómo Java afecta nuestros Diseños

The world “we live” in … ?

Page 4: Cómo Java afecta nuestros Diseños

The world “we live” in … ?

Page 5: Cómo Java afecta nuestros Diseños

The world “we live” in … ?

Page 6: Cómo Java afecta nuestros Diseños

Bret Victor - Thinking the unthinkable

Page 7: Cómo Java afecta nuestros Diseños

Language <-> Thinking

Page 8: Cómo Java afecta nuestros Diseños

What we can not talk about…

we can not think about

(or it is difficult...)

Page 9: Cómo Java afecta nuestros Diseños

Do not Insist on English

Page 10: Cómo Java afecta nuestros Diseños

Aimara: Pasado y Futuro

(nayra) (qhipa)

Page 11: Cómo Java afecta nuestros Diseños
Page 12: Cómo Java afecta nuestros Diseños

What is the relationship with Programming Languages?

(K. Iverson: “Notation as a tool of thought”

1979 ACM Turing Award)

Page 13: Cómo Java afecta nuestros Diseños

If a programming language does not allow me to “TALK”

(write) about certain things

Then I can not THINK about certain

solutions

Page 14: Cómo Java afecta nuestros Diseños

Let’s seeList<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

Page 15: Cómo Java afecta nuestros Diseños

Let’s seeList<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

List<Account> selectedAccounts = new ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

Page 16: Cómo Java afecta nuestros Diseños

Let’s seeList<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

List<Account> selectedAccounts = new ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

What is the problem?

Page 17: Cómo Java afecta nuestros Diseños

We have repeated code!List<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

List<Account> selectedAccounts = new ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

Page 18: Cómo Java afecta nuestros Diseños

Code != Text

Page 19: Cómo Java afecta nuestros Diseños

How do we remove it?

Page 20: Cómo Java afecta nuestros Diseños

Technique:

1. “Contextualize-Copy” the repeated code to some “place”

2. Parameterize what changes

3. NAME IT!!!

4. Use it :-)

Page 21: Cómo Java afecta nuestros Diseños

class Collection<T> {

public Collection<T> <<NAME>> (?) {

List<T> selected = new ArrayList<T> ();

for (T anObject: this )

if ( )

selected.add (anObject);

return selected: }

this

Copy the repeated code to some placeList<Customer> selectedCustomers =

new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add(customer);

return selectedCustomers;

List<Account> selectedAccounts = new

ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

Page 22: Cómo Java afecta nuestros Diseños

Parameterize what changes

List<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

List<Account> selectedAccounts = new ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

How do we do it?

Page 23: Cómo Java afecta nuestros Diseños

An object that represents “code”

Page 24: Cómo Java afecta nuestros Diseños

class Collection<T> {

public Collection<T> <<NAME>> (Closure aClosure) {

List<T> selected = new ArrayList<T> ();

for (T anObject: this )

if ( )

selected.add (anObject);

return selected: }

this

Copy the repeated code to some placeList<Customer> selectedCustomers =

new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add(customer);

return selectedCustomers;

List<Account> selectedAccounts = new

ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

aClosure.execute(anObject)

Page 25: Cómo Java afecta nuestros Diseños

class Collection<T> {

public Collection<T> select (Closure aClosure) {

List<T> selected = new ArrayList<T> ();

for (T anObject: this )

if ( )

selected.add (anObject);

return selected: }

self

NAME IT!List<Customer> selectedCustomers =

new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add(customer);

return selectedCustomers;

List<Account> selectedAccounts = new

ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

aClosure.execute(anObject)

The most difficult part because it means that we understood the repeated code

meaning

Page 26: Cómo Java afecta nuestros Diseños

List<Customer> selectedCustomers = new ArrayList<Customer> ();

for (Customer customer: customers)

if (customer.nameStarsWith(“H”))

selectedCustomers.add (customer);

return selectedCustomers;

List<Account> selectedAccounts = new ArrayList<Account> ();

for (Account account: accounts)

if (account.isOverdraw())

selectedAccounts.add(account);

return selectedAccount;

cutomers.select( customer -> customer.nameStartsWith(“H”) )

accounts.select( account -> account.isOverdraw() )

Page 27: Cómo Java afecta nuestros Diseños

customers.select( new Condition<Customer> () {

public boolean value (Customer aCustomer) {

return aCustomer.nameStartsWith(“H”); }});

cutomers.select( customer -> customber.nameStartsWith(“H”) )

Why not an Anonymous Class?

1. Sintax:

Which one reads better?

2. Binding problems…

Page 28: Cómo Java afecta nuestros Diseños

What did we gain?

1. Few words Simplicity, Easier to

understand, read, remember, etc. Less bugs!

2. We created a new “abstraction”: select (filter in Java 8)

3. … and we removed duplicated code!!

Page 29: Cómo Java afecta nuestros Diseños

Now… let’s do some reflection

1. Why didn’t we see the “duplicated code”?

2. Why didn’t we came with a solution?

Page 30: Cómo Java afecta nuestros Diseños

Why didn’t we see the “duplicated code”

1. Because we are use to that code (is the programming language the problem or us?)

2. Because there is no “concept” to remove it

Page 31: Cómo Java afecta nuestros Diseños

Why didn’t we came with a solution?

1. Because the programming language does not provide us with a “concept” to think about a solution!

Page 32: Cómo Java afecta nuestros Diseños

self

should: [ do something ]

raise: Exception

withExceptionDo: [ :e | self assert: …. ] (Smallalk)

TDD: Test for exceptions

Try {

… do something

fail()

} catch (Exception e) {

assertTrue (…. ) }

Page 33: Cómo Java afecta nuestros Diseños

…and much much more….

Imagine how your designs would be if

you could use closures

You can create your own control flow “sintax”

Further reading: LAMBDA The Ultimate…

Page 34: Cómo Java afecta nuestros Diseños

Meta-Conclusion

Object = ¿Data + Code?

Page 35: Cómo Java afecta nuestros Diseños

Meta-Conclusion

Object = ¿Data + Code?

If you think so, you don´t understand OO yet

Page 36: Cómo Java afecta nuestros Diseños

Mete-Conclusion

Data is an Object

Code is an Object

An Object is a “superior” concept that unifies data and

code

Page 37: Cómo Java afecta nuestros Diseños

Hamming / Closure

If there are certain objects that can not be created in some languages …

… and given the solutions provided by some programming languages…

The statement: “There are design solutions that are unthinkable in some

programming languages”

¿Does it surprise you?

Page 38: Cómo Java afecta nuestros Diseños

Java 8

aList.forEach( ... )

aList.stream().filter( ... )

aList.stream().map( ... )

etc

Lambda != Closure

Examples:

Page 39: Cómo Java afecta nuestros Diseños

What is the problem?

Page 40: Cómo Java afecta nuestros Diseños

No If!

Page 41: Cómo Java afecta nuestros Diseños

Now… let’s do some reflection

1. Why didn’t we see the “polymorphic message”?

2. Why didn’t we came with a right solution?

Page 42: Cómo Java afecta nuestros Diseños

Why did we not see the

“polymorphic message”?

Because 'if' as a reserved word is a “primitive construction”, there is nothing behind it

Therefore it does not make us think in polymorphism

Page 43: Cómo Java afecta nuestros Diseños

If as a message

Page 44: Cómo Java afecta nuestros Diseños
Page 45: Cómo Java afecta nuestros Diseños

A programming language is its creator's state of knowledge

Page 46: Cómo Java afecta nuestros Diseños

timeToRun

Page 47: Cómo Java afecta nuestros Diseños

timeToRun

Page 48: Cómo Java afecta nuestros Diseños

timeToRun

Page 49: Cómo Java afecta nuestros Diseños

How do we “generalize” it?

Page 50: Cómo Java afecta nuestros Diseños

timeToRun

LambdaHelper??

Page 51: Cómo Java afecta nuestros Diseños

timeToRun

Page 52: Cómo Java afecta nuestros Diseños

Extending a Language!

Page 53: Cómo Java afecta nuestros Diseños

How can we add specific behavior to an object?

Let's see an example

Page 54: Cómo Java afecta nuestros Diseños

Now… let’s do some reflection

1. What are the solutions we lack due to limited meta programming?

2. What do we loose if we do not have a meta-circular language?

Page 55: Cómo Java afecta nuestros Diseños

How do we learn a new word?

Page 56: Cómo Java afecta nuestros Diseños

Do we:

1. Stop our brain

2. “edit" a new definition in our dictionary

3. “Compile” the new definition

4. Restart our brain?

Page 57: Cómo Java afecta nuestros Diseños

NO!!

Why do we have to do it with our programs?

Page 58: Cómo Java afecta nuestros Diseños

Can our programs “change while running”?

Can our programs “learn” as we learn?

Page 59: Cómo Java afecta nuestros Diseños

Yes, they can!... If they are

METACIRCULAR

Apply

Eval

Page 60: Cómo Java afecta nuestros Diseños

Let’s see a problem:

Can you make a proxy “learn” about its proxee as it is used?

Page 61: Cómo Java afecta nuestros Diseños

If we don’t have meta-circular languages, then we can not think about solutions that are natural in

our daily lives!!

Page 62: Cómo Java afecta nuestros Diseños

Let’s think again…

Are these concepts new?

Page 63: Cómo Java afecta nuestros Diseños

Lisp

John McCarthy

Alan Kay

“We must know our historyif we don’t want to reinvent… not only the tire, but a a flat tire”

Page 64: Cómo Java afecta nuestros Diseños

And more…

Classes are modules

Static is not OO

Misleading words:

this

extends

Unnecessary implementation complexity

filter, map, etc.

Page 65: Cómo Java afecta nuestros Diseños

Conclusions

Do not accept languages without Closures

Do not accept languages without good meta-programming

Create your own extensions!

Liberate yourself from the programming language

Page 66: Cómo Java afecta nuestros Diseños

Conclusions

Learn other programming languages

Lisp/Scheme

Smalltalk

Forth and more...

Learn our history

Page 67: Cómo Java afecta nuestros Diseños

"The Humble Programmer”

E. Dijkstra

“The tools we are trying to use and the language or notation we are using to express or record our thoughts, are the major factors determining what we can think or express at all!"

Page 68: Cómo Java afecta nuestros Diseños

"Java and C++ make you think that the new ideas are like the old ones. Java is the most distressing thing to hit computing since MS-DOS.“

Alan Kay

Page 69: Cómo Java afecta nuestros Diseños

Questions?

Page 70: Cómo Java afecta nuestros Diseños

agile software development & services

Muchas gracias!

[email protected]

twitter: @10Pines

Argentina

Tel.: +54 (11) 6091-3125Alem 693, 5B(1001) Buenos Aires