Date post: | 14-Aug-2015 |
Category: |
Engineering |
Upload: | commit-software |
View: | 40 times |
Download: | 7 times |
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
Simple design
The code is simple enough when it:0. Runs all the tests1. Contains no duplication2. Expresses every idea that we need to express3. Has the minimum number of classes and functions
(In this order)
Kent Beck
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)
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
Quickly write a test
public class AdderTest { @Test public void testTwoPlusThree() { Adder a = new Adder();
assertEquals(5, a.add(2, 3)); }}
Make it compilepublic 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; }}
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; }}
Make a little testpublic 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; }}
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; }}
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
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.
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