SlideShare a Scribd company logo
1 of 34
Download to read offline
Agile Design Episode 2

Applied Open-Closed
Principle
Andrea Francia

TDD Milano - Tuesday September, 5th 2017
TDD Milano news
Round table delle
communities italiane
all’Agile Day?
Gruppo TDD Milano su
Google Groups
• https://groups.google.com/forum/#!forum/tdd-milano
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/
Learning objectives
• How do we do design during Test Driven Development?

• When to do refactor in TDD?

• How much refactor should we do?
Why design is
important?
Kent Beck. 1999. Extreme Programming Explained: Embrace Change
Legacy Code
Clean Code
When Refactor?
http://agileinaflash.blogspot.it/2009/02/red-green-refactor.html
Refactor last?
• RED
• GREEN
• REFACTOR
Refactor first?
• REFACTOR
• RED
• GREEN
Refactor first and last
• REFACTOR (to make easier add the new feature)
• RED
• GREEN
• REFACTOR (to remove any duplication)
How long to stay in
each phase?
http://agileinaflash.blogspot.it/2009/02/red-green-refactor.html
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
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
http://agileinaflash.blogspot.it/2009/02/red-green-refactor.html
1-2 minutes
1-2 minutes
take your time
The Open/Closed
Principle
The “O” in SOLID
• Single Responsibility Principle (SRP)

• Open/Closed Principle (OCP)
• Liskov substitution Principle (LSP)

• Interface Segregation Principle (ISP)

• Dependency Inversion Principle (DIP)
The Open/Closed Principle
Betrand Meyer’s definition:

“Modules should be both open and closed.”
The Open/Closed Principle
Robert Martin’s definition

“Software entities (classes, modules, functions, etc.)
should be open for extension but closed for
modification.”
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.
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);
};
}
How to learn to apply
OCP?
Kata (repetition)
OCP Kata
Open-Closed Principle Kata: http://matteo.vaccari.name/blog/archives/293
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
OCP Kata Rules
1. Write the first failing test
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.
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.
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
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!
Workshop time
• git clone https://github.com/andreafrancia/ocp-kata.git

• cd ocp-kata

• start-up your IntelliJ IDEA

More Related Content

More from Andrea Francia

Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Andrea Francia
 
Introduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingIntroduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingAndrea Francia
 
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Andrea Francia
 
Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Andrea Francia
 
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Andrea Francia
 
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miTutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miAndrea Francia
 
Writing a Crawler with Python and TDD
Writing a Crawler with Python and TDDWriting a Crawler with Python and TDD
Writing a Crawler with Python and TDDAndrea Francia
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Andrea Francia
 
Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Andrea Francia
 
Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Andrea Francia
 

More from Andrea Francia (14)

Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
Le 12 pratiche - Un introduzione a XP (Mini Italian Agile Day)
 
Introduzione a eXtreme Programming
Introduzione a eXtreme ProgrammingIntroduzione a eXtreme Programming
Introduzione a eXtreme Programming
 
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
Test-Driven Development e Sviluppo Incrementale (TDD-Milano 2017-01-10)
 
Le 12 pratiche
Le 12 praticheLe 12 pratiche
Le 12 pratiche
 
Bash-Only Deployment
Bash-Only DeploymentBash-Only Deployment
Bash-Only Deployment
 
TDD anche su iOS
TDD anche su iOSTDD anche su iOS
TDD anche su iOS
 
Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)Piccolo coding dojo (milano xpug 2013-04-11)
Piccolo coding dojo (milano xpug 2013-04-11)
 
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
Tutti i miei sbagli (Errori di un wannabe Open Source Developer)
 
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG miTutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
Tutti i miei sbagli, versione 7 Marzo 2012 al XPUG mi
 
Writing a Crawler with Python and TDD
Writing a Crawler with Python and TDDWriting a Crawler with Python and TDD
Writing a Crawler with Python and TDD
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 
Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009Subversion @ JUG Milano 11 dic 2009
Subversion @ JUG Milano 11 dic 2009
 
Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)Working Effectively with Legacy Code (draft)
Working Effectively with Legacy Code (draft)
 

Recently uploaded

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 

Recently uploaded (20)

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 

Agile Design Episode 2 - Applied OCP

  • 1. Agile Design Episode 2
 Applied Open-Closed Principle Andrea Francia TDD Milano - Tuesday September, 5th 2017
  • 3. Round table delle communities italiane all’Agile Day?
  • 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?
  • 8. Kent Beck. 1999. Extreme Programming Explained: Embrace Change Legacy Code Clean Code
  • 11. Refactor last? • RED • GREEN • REFACTOR
  • 13. Refactor first and last • REFACTOR (to make easier add the new feature) • RED • GREEN • REFACTOR (to remove any duplication)
  • 14. How long to stay in each phase?
  • 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
  • 20. The “O” in SOLID • Single Responsibility Principle (SRP) • Open/Closed Principle (OCP) • Liskov substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP)
  • 21. The Open/Closed Principle Betrand Meyer’s definition: “Modules should be both open and closed.”
  • 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); }; }
  • 25. How to learn to apply OCP?
  • 27. OCP Kata Open-Closed Principle Kata: http://matteo.vaccari.name/blog/archives/293
  • 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
  • 29. OCP Kata Rules 1. Write the first failing test
  • 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