Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Campagna Anti-IF:
Java e i linguaggi dinamici,
incrementare le potenzialità
espressive di Java
[“Eliminare” i FOR]
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Campagna Anti-IF
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Obiettivo
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License

Più potere a Java “senza” i FOR



Paper su come “eliminare” i FOR
Esperienza di design evolutivo

Attraverso il refactoring

Usando i principi di design
Far vincere qualche maglietta :)
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Sviluppo Java Web: lento!
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License

News [email protected]
http://it.groups.yahoo.com/group/extremeprogramming-it/message/4485:
…
> E poi detta tutta java a sviluppo web non è così tanto veloce in una
> giornata non si riesce.
su questo no :-) Direi che una prima iterazione con tutte le funzionalita'
di base in una giornata con 2 coppie si riesce a fare.
> Con Rails in mezza o 1/4 :) ma non entriamo in guerre di religione.
Se non si riesce probabilmente le storie non sono state spezzate in modo
iterativo-incrementale/verticale ;-)
…
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Il problema
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
Resource resource = null;
(resources select: inForce) do: printSlip.
for(int i=0; i < _resources.size();i++) {
resource = (Resource)_resources.get(i);
if(resource.lastContract().deadline().after(new Date())) {
System.out.println(resource.name());
System.out.println(resource.salary());
}
}
public
void printSlips() {
}
for (Resource eachResource: _resources) {
if(eachResource.lastContract().deadline().after(new Date()) ) {
System.out.println(eachResource.name());
“Our job is to solve
problems, not spoonfeed compilers (…)
We need clarity soSystem.out.println(eachResource.salary());
we can communicate using our code. We value
conciseness }and the ability to express a requirement in code
} and efficiently”.
accurately
--Dave Thomas
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Parliamo di design
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
Resource resource = null;
for(int i=0; i < _resources.size();i++) {
resource = (Resource)_resources.get(i);
if(resource.lastContract().deadline().after(new Date())) {
System.out.println(resource.name());
System.out.println(resource.salary());
}
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Parliamo di design
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
Ciclo?+Navigazione?
Resource resource = null;
for(int i=0; i < _resources.size();i++) {
resource = (Resource)_resources.get(i);
Selezione?
if(resource.lastContract().deadline().after(new Date())) {
System.out.println(resource.name());
Contenuti?
Media?
System.out.println(resource.salary());
}
}
Layout?
}
public void printSlipForEach() {
for (Resource eachResource: _resources) {
if(eachResource.lastContract().deadline().after(new Date()) ) {
System.out.println(eachResource.name());
System.out.println(eachResource.salary());
}
}
} Cirillo – [email protected] MetodiAgili.it
Francesco
Javaday Roma - 01/12/2007
Ciclo?
Abbassare la complessità
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License

Il Refactoring:

Aumentare la capacità del codice di rivelare le
intenzioni di design, a qualsiasi membro del team,
alla prima occhiata

Migliorare la struttura interna del sistema,
consentendo alle necessarie astrazioni di emergere
Lightweight
“Our job is to solve problems, not spoonfeed compilers (…)
We need clarity so we can communicate using our code. We value
conciseness and the ability to express a requirement in code
accurately and efficiently”.
--Dave Thomas
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 1: Separare le logiche
// class Department
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public
void
printSlips()
{
public void printSlips() {
Resource
eachResource = null;
Resource resource
= null;
List inforces = new ArrayList();
for(int i=0; i < _resources.size();i++)
{
i=0; i < _resources.size();i++) {
resource for(int
= (Resource)_resources.get(i);
eachResource = (Resource)_resources.get(i);
Selezione?
if(resource.lastContract().deadline().after(new
Date())) {
if(eachResource.lastContract().deadline().after(new Date()) ) {
System.out.println(resource.name());
Stampa busta?
inforces.add(eachResource);
System.out.println(resource.salary());
}
}
}
}
Resource eachInForce = null;
}
for(int i=0; i < inforces.size();i++) {
eachInForce = (Resource)inforces.get(i);
System.out.println(eachInForce.name());
System.out.println(eachInForce.salary());
}
Francesco Cirillo – }[email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 2: Semplificare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
…
…
Resource eachInForce = null;
Ciclo + navigazione
for(int i=0; i < inforces.size();i++) {
eachInForce = (Resource)inforces.get(i);
System.out.println(eachInForce.name());
Logica i-esima
System.out.println(eachInForce.salary());
}
}
new Block() {
public void evaluate(Resource each) {
System.out.println(each.name());
System.out.println(each.salary());
}}.evaluate(eachInForce);
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 2: Semplificare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
…
…
Resource eachInForce = null;
Ciclo + navigazione
for(int i=0; i < inforces.size();i++) {
eachInForce = (Resource)inforces.get(i);
new Block() {
public void evaluate(Resource each) {
Logica i-esima
System.out.println(each.name());
System.out.println(each.salary());
}}.evaluate(eachInForce);
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 2: Semplificare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
…
…
Resource eachInForce = null;
Ciclo + navigazione
for(int i=0; i < inforces.size();i++) {
eachInForce = (Resource)inforces.get(i);
new PrintSlip().evaluate(eachInForce);
Logica i-esima
}
}
public class PrintSlip implements Block {
public void evaluate(Resource aResource) {
System.out.println(aResource.name());
System.out.println(aResource.salary());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 3: Eliminare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
…
…
Resource eachInForce = null;
Ciclo + navigazione
for(int i=0; i < inforces.size();i++) {
eachInForce = (Resource)inforces.get(i);
new PrintSlip().evaluate(eachInForce);
Logica i-esima
}
}
public class PrintSlip implements Block {
public void evaluate(Resource aResource) {
System.out.println(aResource.name());
System.out.println(aResource.salary());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 3: Eliminare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
…
…
Iterator iterator = inforces.iterator();
Ciclo
while(iterator.hasNext()) {
new PrintSlip().evaluate(iterator.next()); Logica i-esima
}
}
public class PrintSlip implements Block {
public void evaluate(Resource aResource) {
System.out.println(aResource.name());
System.out.println(aResource.salary());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 3: Eliminare il FOR
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips(…) {
…
…
Iterator iterator = inforces.iterator();
Ciclo
while(iterator.hasNext()) {
Logica i-esima
aBlock.evaluate(iterator.next());
}
}
public class PrintSlip implements Block {
public void evaluate(Resource aResource) {
System.out.println(aResource.name());
System.out.println(aResource.salary());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 3: Eliminare il FOR
public class ResourceOrderedCollection {
private Collection _items;
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public ResourceOrderedCollection(Collection items) {
_items = items;
}
Navigazione
public void forEachDo(Block aBlock) {
Ciclo
Iterator iterator = _items.iterator();
while(iterator.hasNext()) {
aBlock.evaluate((Resource)iterator.next());
}
public void printSlips() {
}
Resource eachResource = null;
}
List inforces = new ArrayList();
for(int i=0; i < _resources.size();i++) {
eachResource = (Resource)_resources.get(i);
if(eachResource.lastContract().deadline().after(new Date()) ) {
inforces.add(eachResource);
}
}
new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip());
Francesco Cirillo – [email protected]
}
Javaday Roma - 01/12/2007
Step 4: E la selezione
public void printSlips() {
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Resource eachResource = null;
Ciclo + navigazione
List inforces = new ArrayList();
for(int i=0; i < _resources.size();i++) {
eachResource = (Resource)_resources.get(i);
Selezione
if(eachResource.lastContract().deadline().after(new Date()) ) {
Logica i-esima
inforces.add(eachResource);
Addizione
}
} public void printSlips() {
new ResourceOrderedCollection(inforces).forEachDo(new
PrintSlip());
Resource eachResource = null;
}
List inforces = new ArrayList();
for(int i=0; i < _resources.size();i++) {
eachResource = (Resource)_resources.get(i);
if(new PredicateBlock() {
public boolean is(Resource each) {
return each.lastContract().deadline().after(new Date());
}}.is(eachResource) {
inforces.add(eachResource);
}
}
new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip());
Francesco Cirillo – [email protected] MetodiAgili.it
}
Javaday Roma - 01/12/2007
Step 4: E la selezione
public void printSlips() {
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Resource eachResource = null;
Ciclo + navigazione
List inforces = new ArrayList();
for(int i=0; i < _resources.size();i++) {
eachResource = (Resource)_resources.get(i);
if(new InForcePredicate().is(eachResource)) {
inforces.add(eachResource);
Addizione
}
}
new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip());
}
public class InForcePredicate implements PredicateBlock {
public boolean is(Resource each) {
return each.lastContract().deadline().after(new Date());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Selezione
Step 4: E la selezione
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
Collection inforces = new ResourceOrderedCollection(_resources).select(new InForce());
new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip());
}
public Collection select(PredicateBlock aBlock) {
Collection result = new ArrayList();
Navigazione
Iterator iterator = _items.iterator();
while(iterator.hasNext()) {
Resource resource = (Resource)iterator.next();
if (aBlock.is(resource)) result.add(resource);
}
return result;
}
Ciclo
Addizione
public class InForcePredicate implements PredicateBlock {
public boolean is(Resource each) {
return each.lastContract().deadline().after(new Date());
}
}
Francesco Cirillo – [email protected]
Javaday Roma - 01/12/2007
Selezione
Step 5: Per leggere meglio
public void printSlips() {
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Collection inforces = new ResourceOrderedCollection(_resources).select(new InForce());
new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip());
} public void printSlips() {
(new ResourceOrderedCollection(_resources).select(new InForce()).forEachDo(new PrintSlip());
}
public ResourceOrderedCollection select(PredicateBlock aBlock) {
ResourceOrderedCollection result = new ResourceOrderedCollection();
Iterator iterator = _items.iterator();
while(iterator.hasNext()) {
Resource resource = (Resource)iterator.next();
if (aBlock.is(resource)) result.add(resource);
}
return result;
}
public void printSlips() {
Predicate isInForce = new InForce();
Block printSlip = new PrintSlip();
ResourceOrderedCollection resources = new ResourceOrderedCollection(_resources);
resources.select(isInForce).forEachDo(printSlip);
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 5: Per leggere meglio
public void printSlips() {
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Predicate isInForce = new InForce();
Block printSlip = new PrintSlip();
ResourceOrderedCollection resources = new ResourceOrderedCollection(_resources);
resources.select(isInForce).forEachDo(printSlip);
}
public void printSlips() {
printSlipsUsing(new InForce(),new PrintSlip());
}
private void printSlipsUsing(Predicate aCriterium, Block anAction) {
new ResourceOrderedCollection(_resources).select(aCriterium).forEachDo(anAction);
}
public void printSlipsUsing(Predicate aCriterium, Block anAction) {
_resources.select(aCriterium).forEachDo(anAction);
}
private ResourceOrderedCollection _resources;
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 6: Generalizzare
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip);
public class ResourceOrderedCollection {
…
public void forEachDo(Block aBlock) {
Iterator iterator = _items.iterator();
public class PrintSlip implements Block {
while(iterator.hasNext()) {
public void evaluate(Resource aResource) {
aBlock.evaluate((Resource)iterator.next());
System.out.println(aResource.name());
}
System.out.println(aResource.salary());
}
}
public ResourceOrderedCollection select(Predicate aBlock) {
}
ResourceOrderedCollection result =
new ResourceOrderedCollection();
Iterator iterator = _items.iterator();
while(iterator.hasNext()) {
Resource resource = (Resource)iterator.next();
if (aBlock.is(resource)) result.add(resource);
}
return result;
}
public class InForcePredicate implements PredicateBlock {
}
public boolean is(Resource each) {
return each.lastContract().deadline().after(new Date());
}
}
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Step 6: Generalizzare
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip);
public class ResourceOrderedCollection {
public class OrderedCollection {
…
…
public void forEachDo(Block aBlock)
{
public class InForcePredicate implements
Predicate
{
public void
forEachDo(Block
aBlock) {
Iterator iterator = _items.iterator();
public boolean is(Object anObject)
{
Iterator
iterator = _items.iterator();
while(iterator.hasNext()) {
return ((Resource)anObject).lastContract().deadline().after(new
Date());
while(iterator.hasNext())
{
aBlock.evaluate((Resource)iterator.next());
}
aBlock.evaluate(iterator.next());
}
}
}
}public class PrintSlip implements Block {
public ResourceOrderedCollection select(Predicate aBlock) { }
public void evaluate(Resource aResource) {
public OrderedCollection select(Predicate aBlock) {
ResourceOrderedCollection result =
System.out.println(aResource.name());
OrderedCollection result =
new
ResourceOrderedCollection();
new OrderedCollection();
IteratorSystem.out.println(aResource.salary());
iterator = _items.iterator();
Iterator iterator = _items.iterator();
}
while(iterator.hasNext())
{
while(iterator.hasNext()) {
}
Resource resource = (Resource)iterator.next();
ObjectBlock
object{ = iterator.next();
public class PrintSlip implements
if (aBlock.is(resource)) result.add(resource);
if (aBlock.is(object))
public void evaluate(Object
anObject) { result.add(object);
}
}
System.out.println(((Resource)anObject).name());
return result;
return result;
System.out.println(((Resource)anObject).salary());}
}
}
} {
}public class InForcePredicate implements PredicateBlock
}
public boolean is(Resource each) {
return each.lastContract().deadline().after(new Date());
}
Francesco
Cirillo – [email protected] MetodiAgili.it
}
Javaday Roma - 01/12/2007
Dove siamo
// class Department
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
public void printSlips() {
Navigazione?
Resource resource = null;
for(int i=0; i < _resources.size();i++) {
resource = (Resource)_resources.get(i);
Selezione?
if(resource.lastContract().deadline().after(new Date())) {
System.out.println(resource.name());
Contenuti?
Media?
System.out.println(resource.salary());
}
}
Layout?
}
_resources.select(onlyWomen).forEachDo(printReport);
_resources.select(isInForce).forEachDo(printSlip);
Navigazione
Selezione
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Contenuti+
Media+
Layout
// class Department
public void printSlips() {
Resource resource = null;
Navigazione?
for(int i=0; i < _resources.size();i++) {
resource = (Resource)_resources.get(i);
Selezione?
if(resource.lastContract().deadline().after(new Date())) {
System.out.println(resource.name());
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Contenuti?
Media?
System.out.println(resource.salary());
}
}
}
Review & ToDoList
Layout?
10/01-10/11/2007:Contract
Andrea:Employee
“Andrea”:String
:Department
Federica:Manager
1/9/2007-10/10/2008:Contract
System.out:PrintStream
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
“Federica”:String
Review & ToDoList
public OrderedCollection select(Predicate aBlock) {
OrderedCollection result = new OrderedCollection();
2.5 License
publicCreative
booleanCommons
is(Object Attribution-NonCommercial-ShareAlike
each) {
Iterator iterator = _items.iterator();
return ((Resource)each).lastContract().deadline().after(new Date());
_resources.select(isInForce).forEachDo(printSlip);
while(iterator.hasNext()) {
}
Object object = iterator.next();
if (aBlock.is(object)) result.add(object);
}
return result;
10/01-10/11/2007:Contract
}
resources:OrderedCollection
Andrea:Employee
“Andrea”:String
:InForce
:Department
inForce:OrderedCollection
Federica:Manager
1/9/2007-10/10/2008:Contract
:PrintSlip
“Federica”:String
System.out:PrintStream
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Review & ToDoList
public void forEachDo(Block aBlock) {
Iterator iterator = _items.iterator();
while(iterator.hasNext()) {
aBlock.evaluate(iterator.next());
}
}
public OrderedCollection select(Predicate aBlock) {
publicOrderedCollection
class PrintSlip implements
Block
{
result = new
OrderedCollection();
Creative
Commons
Attribution-NonCommercial-ShareAlike
2.5 License
public
void
evaluate(Object
anObject) {
Iterator
iterator
= _items.iterator();
System.out.println(((Resource)anObject).name());
while(iterator.hasNext())
{
System.out.println(((Resource)anObject).salary());
Object
object = iterator.next();
if (aBlock.is(object)) result.add(object);
}
}
}
return result;
}
10/01-10/11/2007:Contract
resources:OrderedCollection
Andrea:Employee
“Andrea”:String
:InForce
:Department
inForce:OrderedCollection
Federica:Manager
1/9/2007-10/10/2008:Contract
:PrintSlip
“Federica”:String
System.out:PrintStream
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Riferimenti blocchi e chiusure

Blocchi in Java


http://www.artima.com/intv/closuresP.html
Chiusure nel linguaggio Java


http://www.c2.com/cgi/wiki?BlocksInJava
Blocchi e chiusure in Ruby


Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
http://www.javac.info/closures-v03.html
Esempi di librerie

http://jaggregate.sourceforge.net
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Segui la Campagna Anti-IF
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Domande
Risorse:
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
http://www.xplabs.it/risorse.html
http://www.metodiagili.it/campagna-anti-if.html
Tecnica del Pomodoro:
http://www.tecnicadelpomodoro.it
EasyTracking:
http://www.myagileprojects.com
Gruppo XPLabs’ Friends- Italia:
http://it.groups.yahoo.com/group/xplabs-friends/
Extreme Programming User Group – Italia:
http://www.xpug.it/
Francesco Cirillo – [email protected] MetodiAgili.it
Javaday Roma - 01/12/2007
Scarica

- makesimple