SlideShare a Scribd company logo
Object
Calisthenics
#AgileVenturePrato2018
Ferdinando Santacroce
@jesuswasrasta
“
2
The word calisthenics comes from
the ancient Greek words kalòs
(καλός), beauty, and sthénos
(σθένος), strength.
@jesuswasrasta #AgileVenturePrato2018
“
3
So, here’s an exercise that can help you to
internalize principles of good object-oriented
design and actually use them in real life.
Jeff Bay
bit.ly/ObjectCalisthenics
@jesuswasrasta #AgileVenturePrato2018
4
class Board {
public String print() {
StringBuilder buffer = new StringBuilder();
//Level 0
for (int i = 0; i < 10; i++) {
//Level 1: OK
for (int j = 0; j < 10; j++) {
//Level 2: NOT ALLOWED!
buffer.append(data[i][j]);
}
buffer.append("n");
}
return buffer.toString();
}
}
1. Only one level of indentation per method
@jesuswasrasta #AgileVenturePrato2018
5
if(status==Status.DONE){
doSomething();
} else {//ELSE NOT ALLOWED!
doSomethingElse();
}
2. Don’t use ELSE keyword
@jesuswasrasta #AgileVenturePrato2018
6
3. Wrap all primitives and strings
class ZipCode{
String regex = "^[0-9]{5}(?:-[0-9]{4})?$";
Pattern pattern = Pattern.compile(regex);
String value;
public ZipCode(String value) {
this.value = value;
}
public boolean isWellFormed(){
return pattern.matcher(value).matches();
}
}
//Don't use primitives!
String regex = "^[0-9]{5}(?:-[0-9]{4})?$";
Pattern pattern = Pattern.compile(regex);
String zip = "12345";
if (pattern.matcher(value).matches()){
//...
}
//Use objects, we are doing OOP, after
all..
ZipCode zipCode = new ZipCode("12345");
if(zipCode.isWellFormed()){
//...
}
@jesuswasrasta #AgileVenturePrato2018
7
//Don't use collections, directly
List<String> players = new ArrayList<>();
String player = "John";
if(!(players.contains(player) || players.size() == 5)){
players.add(player);
}
4. First class collections (1)
@jesuswasrasta #AgileVenturePrato2018
8
//Wrap collections in meaningful objects
Team team = new Team();
team.add(player);
//...
class Team {
List<String> players = new ArrayList<>();
final int TEAM_SIZE = 5;
public void add(String player){
if(players.contains(player)){
throw new DuplicatePlayer("Player " + player + " already present");
}
if(players.size() == TEAM_SIZE){
throw new TeamFull("Team full, max " + TEAM_SIZE + " players");
}
players.add(player);
}
4. First class collections (2)
@jesuswasrasta #AgileVenturePrato2018
9
public void update(){
//Only one dot per line
context.getSession().update();
//That doesn't means this...
Session session = context.getSession();
session.update();
//But this...
context.update();
}
5. One dot per line
@jesuswasrasta #AgileVenturePrato2018
10
//Don't do this
String signOcr = "John Doe";
srep(signOcr);
//But this
String signatureFromOcr = "John Doe";
signReport(signatureFromOcr);
6. Don’t abbreviate
@jesuswasrasta #AgileVenturePrato2018
11
7. Keep all entities small
//But small and cohesive ones like these!
class Player{
Identity identity;
Address address;
public Player(Identity identity){
this.identity = identity;
}
public changeAddress(Address address){
this.address = address;
}
}
//Don’t create this kind of entities...
class Player{
String firstName;
String lastName;
String nickName;
String city;
String street;
String streetNumber;
String zipCode;
public void setFirstName(String firstName){
this.firstName = firstName;
}
//...
//And so on, tons of getters and setter
//...
}
class Identity{
String firstName;
String lastName;
String nickName;
}
class Address{
String city;
String street;
String streetNumber;
String zipCode;
}
@jesuswasrasta #AgileVenturePrato2018
12
8. No classes with more than two instance variables
//But you have to do this!
class Identity{
FamilyName familyName;
GivenName givenName;
}
//So, you can’t do this...
class Identity{
String firstName;
String lastName;
String nickName;
} class FamilyName{
String firstName;
String lastName;
}
class GivenName{
String nickName;
}
@jesuswasrasta #AgileVenturePrato2018
13
9. No getters/setters/properties
//You are not allowed to do this...
class Player{
String firstName;
//...
public String zipCode;
public void getFirstName(String firstName){
return this.firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
//And so on...
} @jesuswasrasta #AgileVenturePrato2018
14
Time to get stronger!
Setup
Tennis Game Kata
3-4 pomodoros
Pair programming
TDD
Debriefing, Q&A
@jesuswasrasta #AgileVenturePrato2018
15
https://github.com/jesuswasrasta/
Tennis-Refactoring-Kata
LET’S START!
@jesuswasrasta #AgileVenturePrato2018
16
@jesuswasrasta #AgileVenturePrato2018
Most simple/difficult rule to respect?
Too many objects?
Did rules conflicted, sometimes?
Does it felt weird or uncomfortable, maybe?
Debriefing
17
Summing up...
18
Encapsulation
19
Polimorphism
20
Naming
21
1. Only one level of indentation per method
Promotes method cohesi-veness
Single Responsibility Principle (S.O.L.I.D.)
Extract method
Extract objects (collaborators)
@jesuswasrasta #AgileVenturePrato2018
22
2. Don’t use ELSE keyword
Polymorphism
Behavioral Patterns
Strategy Pattern (set algorithm at runtime)
State Pattern (state machines)
@jesuswasrasta #AgileVenturePrato2018
23
3. Wrap all primitives and strings
Primitive Obsession Anti-Pattern:
Using primitives to represent domain ideas
@jesuswasrasta #AgileVenturePrato2018
24
4. First class collections
Encapsulation:
Any class that contains a collection
should contain no other member variables
Give behaviors related to the collection a home
@jesuswasrasta #AgileVenturePrato2018
25
5. One dot per line
Law of Demeter
Only talk with your immediate friends,
don’t talk to strangers
Fluent interfaces allowed
@jesuswasrasta #AgileVenturePrato2018
26
6. Don’t abbreviate
Naming
“Clean Code” book, Robert C. Martin, Ch. 2
Meaningful, intention-revealing,
pronounceable, searchable names
Method’s name too long?
Maybe it does too many things...
@jesuswasrasta #AgileVenturePrato2018
27
7. Keep all entities small
Single Responsibility Principle (again...)
Class with 50 l.o.c, methods with 5
The “class that fits in the monitor” rule
There’s no black & white, it’s all about trade-offs
@jesuswasrasta #AgileVenturePrato2018
28
8. No classes with more than two instance variables
High cohesion, low coupling
@jesuswasrasta #AgileVenturePrato2018
29
9. No getters/setters/properties
Tell, don’t ask
Feature envy code-smell
(extensive use of another class)
They violates Open/Closed Principle (S.O.L.I.D.)
Anemic Domain anti-pattern
@jesuswasrasta #AgileVenturePrato2018
30
Thanks!
Any questions?
You can find me at:
▫ @jesuswasrasta
▫ https://about.me/ferdinando.santacroce

More Related Content

Similar to Object Calisthenics - Agile Venture Prato 2018

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
Naresh Jain
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
zenMonkey
 
Fewd week6 slides
Fewd week6 slidesFewd week6 slides
Fewd week6 slides
William Myers
 
How Good of a Java Developer are You?
How Good of a Java Developer are You?How Good of a Java Developer are You?
How Good of a Java Developer are You?
Sherif Koussa
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
ARORACOCKERY2111
 
Jeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean CodeJeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean Code
Awesome Ars Academia
 
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdfThis is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
jillisacebi75827
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basics
Joseph Burchett
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with Groovy
Ali Tanwir
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
NexThoughts Technologies
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
rd1742
 
Clean code
Clean codeClean code
Clean code
Khou Suylong
 
Manipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtimeManipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtime
Andrei Ursan
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamic
Antoine Sabot-Durand
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
MikeAdva
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenment
Artur Szott
 
Template Method Design Pattern
Template Method Design PatternTemplate Method Design Pattern
Template Method Design Pattern
Srikanth Shreenivas
 
Adopting F# at SBTech
Adopting F# at SBTechAdopting F# at SBTech
Adopting F# at SBTech
Antya Dev
 

Similar to Object Calisthenics - Agile Venture Prato 2018 (20)

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
 
Fewd week6 slides
Fewd week6 slidesFewd week6 slides
Fewd week6 slides
 
How Good of a Java Developer are You?
How Good of a Java Developer are You?How Good of a Java Developer are You?
How Good of a Java Developer are You?
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
 
Jeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean CodeJeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean Code
 
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdfThis is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basics
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with Groovy
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
 
Clean code
Clean codeClean code
Clean code
 
Manipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtimeManipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtime
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamic
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenment
 
Template Method Design Pattern
Template Method Design PatternTemplate Method Design Pattern
Template Method Design Pattern
 
Adopting F# at SBTech
Adopting F# at SBTechAdopting F# at SBTech
Adopting F# at SBTech
 

More from Ferdinando Santacroce

Continuous Delivery
Continuous DeliveryContinuous Delivery
Continuous Delivery
Ferdinando Santacroce
 
Growing Teams - Agile Venture Milano - #avmi2020
Growing Teams - Agile Venture Milano - #avmi2020Growing Teams - Agile Venture Milano - #avmi2020
Growing Teams - Agile Venture Milano - #avmi2020
Ferdinando Santacroce
 
Testare l'intestabile - Italian Agile Days 2019 #IAD19
Testare l'intestabile - Italian Agile Days 2019 #IAD19Testare l'intestabile - Italian Agile Days 2019 #IAD19
Testare l'intestabile - Italian Agile Days 2019 #IAD19
Ferdinando Santacroce
 
I'm a mediocre developer
I'm a mediocre developerI'm a mediocre developer
I'm a mediocre developer
Ferdinando Santacroce
 
Working software
Working softwareWorking software
Working software
Ferdinando Santacroce
 
I'm a mediocre developer
I'm a mediocre developerI'm a mediocre developer
I'm a mediocre developer
Ferdinando Santacroce
 
TDD a piccoli passi
TDD a piccoli passiTDD a piccoli passi
TDD a piccoli passi
Ferdinando Santacroce
 
Agile versioning with Git
Agile versioning with GitAgile versioning with Git
Agile versioning with Git
Ferdinando Santacroce
 

More from Ferdinando Santacroce (8)

Continuous Delivery
Continuous DeliveryContinuous Delivery
Continuous Delivery
 
Growing Teams - Agile Venture Milano - #avmi2020
Growing Teams - Agile Venture Milano - #avmi2020Growing Teams - Agile Venture Milano - #avmi2020
Growing Teams - Agile Venture Milano - #avmi2020
 
Testare l'intestabile - Italian Agile Days 2019 #IAD19
Testare l'intestabile - Italian Agile Days 2019 #IAD19Testare l'intestabile - Italian Agile Days 2019 #IAD19
Testare l'intestabile - Italian Agile Days 2019 #IAD19
 
I'm a mediocre developer
I'm a mediocre developerI'm a mediocre developer
I'm a mediocre developer
 
Working software
Working softwareWorking software
Working software
 
I'm a mediocre developer
I'm a mediocre developerI'm a mediocre developer
I'm a mediocre developer
 
TDD a piccoli passi
TDD a piccoli passiTDD a piccoli passi
TDD a piccoli passi
 
Agile versioning with Git
Agile versioning with GitAgile versioning with Git
Agile versioning with Git
 

Recently uploaded

Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Envertis Software Solutions
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 

Recently uploaded (20)

Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 

Object Calisthenics - Agile Venture Prato 2018

  • 2. “ 2 The word calisthenics comes from the ancient Greek words kalòs (καλός), beauty, and sthénos (σθένος), strength. @jesuswasrasta #AgileVenturePrato2018
  • 3. “ 3 So, here’s an exercise that can help you to internalize principles of good object-oriented design and actually use them in real life. Jeff Bay bit.ly/ObjectCalisthenics @jesuswasrasta #AgileVenturePrato2018
  • 4. 4 class Board { public String print() { StringBuilder buffer = new StringBuilder(); //Level 0 for (int i = 0; i < 10; i++) { //Level 1: OK for (int j = 0; j < 10; j++) { //Level 2: NOT ALLOWED! buffer.append(data[i][j]); } buffer.append("n"); } return buffer.toString(); } } 1. Only one level of indentation per method @jesuswasrasta #AgileVenturePrato2018
  • 5. 5 if(status==Status.DONE){ doSomething(); } else {//ELSE NOT ALLOWED! doSomethingElse(); } 2. Don’t use ELSE keyword @jesuswasrasta #AgileVenturePrato2018
  • 6. 6 3. Wrap all primitives and strings class ZipCode{ String regex = "^[0-9]{5}(?:-[0-9]{4})?$"; Pattern pattern = Pattern.compile(regex); String value; public ZipCode(String value) { this.value = value; } public boolean isWellFormed(){ return pattern.matcher(value).matches(); } } //Don't use primitives! String regex = "^[0-9]{5}(?:-[0-9]{4})?$"; Pattern pattern = Pattern.compile(regex); String zip = "12345"; if (pattern.matcher(value).matches()){ //... } //Use objects, we are doing OOP, after all.. ZipCode zipCode = new ZipCode("12345"); if(zipCode.isWellFormed()){ //... } @jesuswasrasta #AgileVenturePrato2018
  • 7. 7 //Don't use collections, directly List<String> players = new ArrayList<>(); String player = "John"; if(!(players.contains(player) || players.size() == 5)){ players.add(player); } 4. First class collections (1) @jesuswasrasta #AgileVenturePrato2018
  • 8. 8 //Wrap collections in meaningful objects Team team = new Team(); team.add(player); //... class Team { List<String> players = new ArrayList<>(); final int TEAM_SIZE = 5; public void add(String player){ if(players.contains(player)){ throw new DuplicatePlayer("Player " + player + " already present"); } if(players.size() == TEAM_SIZE){ throw new TeamFull("Team full, max " + TEAM_SIZE + " players"); } players.add(player); } 4. First class collections (2) @jesuswasrasta #AgileVenturePrato2018
  • 9. 9 public void update(){ //Only one dot per line context.getSession().update(); //That doesn't means this... Session session = context.getSession(); session.update(); //But this... context.update(); } 5. One dot per line @jesuswasrasta #AgileVenturePrato2018
  • 10. 10 //Don't do this String signOcr = "John Doe"; srep(signOcr); //But this String signatureFromOcr = "John Doe"; signReport(signatureFromOcr); 6. Don’t abbreviate @jesuswasrasta #AgileVenturePrato2018
  • 11. 11 7. Keep all entities small //But small and cohesive ones like these! class Player{ Identity identity; Address address; public Player(Identity identity){ this.identity = identity; } public changeAddress(Address address){ this.address = address; } } //Don’t create this kind of entities... class Player{ String firstName; String lastName; String nickName; String city; String street; String streetNumber; String zipCode; public void setFirstName(String firstName){ this.firstName = firstName; } //... //And so on, tons of getters and setter //... } class Identity{ String firstName; String lastName; String nickName; } class Address{ String city; String street; String streetNumber; String zipCode; } @jesuswasrasta #AgileVenturePrato2018
  • 12. 12 8. No classes with more than two instance variables //But you have to do this! class Identity{ FamilyName familyName; GivenName givenName; } //So, you can’t do this... class Identity{ String firstName; String lastName; String nickName; } class FamilyName{ String firstName; String lastName; } class GivenName{ String nickName; } @jesuswasrasta #AgileVenturePrato2018
  • 13. 13 9. No getters/setters/properties //You are not allowed to do this... class Player{ String firstName; //... public String zipCode; public void getFirstName(String firstName){ return this.firstName; } public void setFirstName(String firstName){ this.firstName = firstName; } //And so on... } @jesuswasrasta #AgileVenturePrato2018
  • 14. 14 Time to get stronger! Setup Tennis Game Kata 3-4 pomodoros Pair programming TDD Debriefing, Q&A @jesuswasrasta #AgileVenturePrato2018
  • 16. 16 @jesuswasrasta #AgileVenturePrato2018 Most simple/difficult rule to respect? Too many objects? Did rules conflicted, sometimes? Does it felt weird or uncomfortable, maybe? Debriefing
  • 21. 21 1. Only one level of indentation per method Promotes method cohesi-veness Single Responsibility Principle (S.O.L.I.D.) Extract method Extract objects (collaborators) @jesuswasrasta #AgileVenturePrato2018
  • 22. 22 2. Don’t use ELSE keyword Polymorphism Behavioral Patterns Strategy Pattern (set algorithm at runtime) State Pattern (state machines) @jesuswasrasta #AgileVenturePrato2018
  • 23. 23 3. Wrap all primitives and strings Primitive Obsession Anti-Pattern: Using primitives to represent domain ideas @jesuswasrasta #AgileVenturePrato2018
  • 24. 24 4. First class collections Encapsulation: Any class that contains a collection should contain no other member variables Give behaviors related to the collection a home @jesuswasrasta #AgileVenturePrato2018
  • 25. 25 5. One dot per line Law of Demeter Only talk with your immediate friends, don’t talk to strangers Fluent interfaces allowed @jesuswasrasta #AgileVenturePrato2018
  • 26. 26 6. Don’t abbreviate Naming “Clean Code” book, Robert C. Martin, Ch. 2 Meaningful, intention-revealing, pronounceable, searchable names Method’s name too long? Maybe it does too many things... @jesuswasrasta #AgileVenturePrato2018
  • 27. 27 7. Keep all entities small Single Responsibility Principle (again...) Class with 50 l.o.c, methods with 5 The “class that fits in the monitor” rule There’s no black & white, it’s all about trade-offs @jesuswasrasta #AgileVenturePrato2018
  • 28. 28 8. No classes with more than two instance variables High cohesion, low coupling @jesuswasrasta #AgileVenturePrato2018
  • 29. 29 9. No getters/setters/properties Tell, don’t ask Feature envy code-smell (extensive use of another class) They violates Open/Closed Principle (S.O.L.I.D.) Anemic Domain anti-pattern @jesuswasrasta #AgileVenturePrato2018
  • 30. 30 Thanks! Any questions? You can find me at: ▫ @jesuswasrasta ▫ https://about.me/ferdinando.santacroce