cómo java afecta nuestros diseños
DESCRIPTION
Presentacion de la charla de JavaConf 2014TRANSCRIPT
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
The world “we live” in … ?
The world “we live” in … ?
The world “we live” in … ?
The world “we live” in … ?
Bret Victor - Thinking the unthinkable
Language <-> Thinking
What we can not talk about…
we can not think about
(or it is difficult...)
Do not Insist on English
Aimara: Pasado y Futuro
(nayra) (qhipa)
What is the relationship with Programming Languages?
(K. Iverson: “Notation as a tool of thought”
1979 ACM Turing Award)
If a programming language does not allow me to “TALK”
(write) about certain things
…
Then I can not THINK about certain
solutions
Let’s seeList<Customer> selectedCustomers = new ArrayList<Customer> ();
for (Customer customer: customers)
if (customer.nameStarsWith(“H”))
selectedCustomers.add (customer);
return selectedCustomers;
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;
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?
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;
Code != Text
How do we remove it?
Technique:
1. “Contextualize-Copy” the repeated code to some “place”
2. Parameterize what changes
3. NAME IT!!!
4. Use it :-)
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;
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?
An object that represents “code”
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)
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
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() )
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…
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!!
Now… let’s do some reflection
1. Why didn’t we see the “duplicated code”?
2. Why didn’t we came with a solution?
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
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!
self
should: [ do something ]
raise: Exception
withExceptionDo: [ :e | self assert: …. ] (Smallalk)
TDD: Test for exceptions
Try {
… do something
fail()
} catch (Exception e) {
assertTrue (…. ) }
…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…
Meta-Conclusion
Object = ¿Data + Code?
Meta-Conclusion
Object = ¿Data + Code?
If you think so, you don´t understand OO yet
Mete-Conclusion
Data is an Object
Code is an Object
An Object is a “superior” concept that unifies data and
code
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?
Java 8
aList.forEach( ... )
aList.stream().filter( ... )
aList.stream().map( ... )
etc
Lambda != Closure
Examples:
What is the problem?
No If!
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?
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
If as a message
A programming language is its creator's state of knowledge
timeToRun
timeToRun
timeToRun
How do we “generalize” it?
timeToRun
LambdaHelper??
timeToRun
Extending a Language!
How can we add specific behavior to an object?
Let's see an example
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?
How do we learn a new word?
Do we:
1. Stop our brain
2. “edit" a new definition in our dictionary
3. “Compile” the new definition
4. Restart our brain?
NO!!
Why do we have to do it with our programs?
Can our programs “change while running”?
Can our programs “learn” as we learn?
Yes, they can!... If they are
METACIRCULAR
Apply
Eval
Let’s see a problem:
Can you make a proxy “learn” about its proxee as it is used?
If we don’t have meta-circular languages, then we can not think about solutions that are natural in
our daily lives!!
Let’s think again…
Are these concepts new?
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”
And more…
Classes are modules
Static is not OO
Misleading words:
this
extends
Unnecessary implementation complexity
filter, map, etc.
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
Conclusions
Learn other programming languages
Lisp/Scheme
Smalltalk
Forth and more...
Learn our history
"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!"
"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
Questions?
agile software development & services
Muchas gracias!
twitter: @10Pines
Argentina
Tel.: +54 (11) 6091-3125Alem 693, 5B(1001) Buenos Aires