14. Clean code that works
• is out of reach of even the best
programmers, some of the time,
• and out of reach of most programmers
(like me) most of the time
-- Kent Beck
15. Simple design
The code is simple enough when it:
0. Runs all the tests
1. Contains no duplication
2. Expresses every idea that we need to express
3. Has the minimum number of classes and functions
(In this order)
Kent Beck
18. Design semplice: spezzare le
cose in maniera che i pezzi
• Siano comprensibili in isolamento (coese -
rappresentano un concetto)
• Siano componibili in nuove maniere
(disaccoppiate e riusabili)
19. Test-Driven
Development
1. Quickly add a test
2. Run all the tests, see the new one fail
3. Make a little change
4. Run all the tests, see them all succeed
5. Refactor to remove duplication
20. Quickly write a test
public class AdderTest {
@Test
public void testTwoPlusThree() {
Adder a = new Adder();
assertEquals(5, a.add(2, 3));
}
}
21. Make it compile
public class AdderTest {
@Test
public void testTwoPlusThree() {
Adder a = new Adder();
assertEquals(5, a.add(2, 3));
}
}
public class Adder {
public int add(int a, int b) { return 0; }
}
22. See it fail!
Expected 5, was 0
public class AdderTest {
@Test
public void testTwoPlusThree() {
Adder a = new Adder();
assertEquals(5, a.add(2, 3));
}
}
public class Adder {
public int add(int a, int b) { return 0; }
}
23. Make a little test
public class AdderTest {
@Test
public void testTwoPlusThree() {
Adder a = new Adder();
assertEquals(5, a.add(2, 3));
}
}
public class Adder {
public int add(int a, int b) { return 5; }
}
24. Refactor to remove
duplication
public class AdderTest {
@Test
public void testTwoPlusThree() {
Adder a = new Adder();
assertEquals(5, a.add(2, 3));
}
}
public class Adder {
public int add(int a, int b) { return a+b; }
}
25. The procedure
1. Write a test
2. Make it compile
3. Make it pass
4. Refactor
Expected 5, was 0
27. Demo: anni bisestili
• Un anno è bisestile se è divisibile per 4:
1996 è bisestile; 1995 non è bisestile
• A meno che non sia divisibile per 100:
1900 non è bisestile
• A meno che non sia divisibile per 400:
2000 è bisestile
Emily Bache, Coding Dojo Handbook
28. Problema: Monopoli
Vogliamo realizzare un’applicazione
che gioca a Monopoli. Il computer
simula un certo numero di
avversari e permette a un singolo
utente di giocare.
29. Monopoli semplificato
• Simuliamo 4 giocatori
• La simulazione prosegue per 20 round
• Un round consiste nel turno di tutti i giocatori
• Il turno di un giocatore consiste nel lanciare i dadi
e muovere il segnalino
• Le caselle si chiamano “Go!”,“Square 1”,
“Square 2”, ...“Square 39”
• Non ci sono né soldi, né vincitori
Formulazione di Craig Larman, Applying UML and patterns