4. Gruppo TDD Milano su
Google Groups
• https://groups.google.com/forum/#!forum/tdd-milano
5. Le prossime conferenze
Agile Business Day
Venerdì 16 Settembre 2017 a Venezia
http://www.agilebusinessday.com/
Codemotion Milano
8 e 9 novembre 2017 a Milano
http://milan2017.codemotionworld.com/
Italian Agile Day
venerdì 17 e sabato 18 novembre 2017 a Urbino
http://www.agileday.it/
NoSlidesConf
sabato 25 novembre 2017, a Bologna
http://www.noslidesconf.net/
6. Learning objectives
• How do we do design during Test Driven Development?
• When to do refactor in TDD?
• How much refactor should we do?
16. The TDD Rhythm:
1. Quickly add a test
2. Run all tests and see the new one fail
3. Make a little change
4. Run all tests and see them all succeed
5. Refactor to remove duplication
17. The TDD Rhythm:
1. Quickly add a test
2. Run all tests and see the new one fail
3. Make a little change
4. Run all tests and see them all succeed
5. Refactor to remove duplication
22. The Open/Closed Principle
Robert Martin’s definition
“Software entities (classes, modules, functions, etc.)
should be open for extension but closed for
modification.”
23. Open and Closed
• Open for extension means we can extend the module
with new behaviors that implements new requirements
(we can change what the module does).
• Closed for modification means that extending the
behavior of a module does not result in changes to the
source, or binary, code of the module.
24. Open/Closed Principle
found in the wild
public static void main(String[] args) {
List<Integer> list = asList(3, 2, 1);
// standard sort
Collections.sort(list);
printAll(list);
// sorting with a specific comparator
Collections.sort(list, pronounceComparator());
printAll(list);
}
private static Comparator<Integer> pronounceComparator() {
return (o1, o2) -> {
String s1 = pronounce(o1);
String s2 = pronounce(o2);
return s1.compareTo(s2);
};
}
28. When do we refactor?
Starting code base Changes implemented
red == code changed
(Hopefully) Code cleaned up
Refactor
after
Starting code base Change design to make
room for new feature
Implement feature
Refactor
before
From a slide by Dave Nicolette
30. OCP Kata Rules
1. Write the first failing test
2. Then write a factory that returns an object, or an
aggregate of objects, that make the test pass.
31. OCP Kata Rules
1. Write the first failing test
2. Then write a factory that returns an object, or an
aggregate of objects, that make the test pass.
3. Write the next failing test.
32. OCP Kata Rules
1. Write the first failing test
2. Then write a factory that returns an object, or an
aggregate of objects, that make the test pass.
3. Write the next failing test.
4. Make the test pass adding, changing (or deleting) just
one object in the factory
33. OCP Kata Rules
1. Write the first failing test
2. Then write a factory that returns an object, or an
aggregate of objects, that make the test pass.
3. Write the next failing test.
4. Make the test pass adding, changing (or deleting) just
one object in the factory
You can’t? Then refactor your code until you can!
34. Workshop time
• git clone https://github.com/andreafrancia/ocp-kata.git
• cd ocp-kata
• start-up your IntelliJ IDEA