SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à Object Calisthenics - Agile Venture Prato 2018

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailszenMonkey
 
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.pdfARORACOCKERY2111
 
Jeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean CodeJeremiah Caballero - Introduction of Clean Code
Jeremiah Caballero - Introduction of Clean CodeAwesome 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.pdfjillisacebi75827
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basicsJoseph Burchett
 
Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with GroovyAli Tanwir
 
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.pdfrd1742
 
Manipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtimeManipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtimeAndrei 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 dynamicAntoine Sabot-Durand
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.pptMikeAdva
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentArtur Szott
 
Adopting F# at SBTech
Adopting F# at SBTechAdopting F# at SBTech
Adopting F# at SBTechAntya Dev
 

Similaire à 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
 

Plus de Ferdinando Santacroce

Plus de 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
 

Dernier

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 

Dernier (20)

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

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