SlideShare a Scribd company logo
1 of 41
Object
Calisthenics
Improve your code following 9 rules
Jeff Bay
Jeff Bay
2008
How long time do you
spend reading code?
How you improve
readability?
Where to start?
Object Calisthenics
One level of indentation
per method
1
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
// 0
for (int i = 0; i < 10; i++) {
// 1
for (int j = 0; j < 10; j++) {
// 2
buf.append(data[i][j]);
}
buf.append("n");
}
return buf.toString();
}
}
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
// 0
for (int i = 0; i < 10; i++) {
// 1
for (int j = 0; j < 10; j++) {
// 2
buf.append(data[i][j]);
}
buf.append("n");
}
return buf.toString();
}
} Extract Method
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
collectRows(buf);
return buf.toString();
}
private void collectRows(StringBuilder buf) {
for (int i = 0; i < 10; i++) {
collectRow(buf, i);
}
}
private void collectRow(StringBuilder buf, int row) {
for (int i = 0; i < 10; i++) {
buf.append(data[row][i]);
}
buf.append("n");
}
}
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
// 0
for (int i = 0; i < 10; i++) {
// 1
for (int j = 0; j < 10; j++) {
// 2
buf.append(data[i][j]);
}
buf.append("n");
}
return buf.toString();
}
}
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
collectRows(buf);
return buf.toString();
}
}
Don't use ELSE
2
public void login(String username, String password) {
if (userRepository.isValid(username, password)) {
redirect("homepage");
} else {
addFlash("error", "Bad credentials");
redirect("login");
}
}
Early return
public void login(String username, String password) {
if (userRepository.isValid(username, password)) {
redirect("homepage");
} else {
addFlash("error", "Bad credentials");
redirect("login");
}
}
public void login(String username, String password) {
if (userRepository.isValid(username, password)) {
return redirect("homepage");
}
addFlash("error", "Bad credentials");
return redirect("login");
}
Wrap all primitives and
Strings in classes
3
public void direction(int long, int lat)
{...}
public void direction(int long, int lat)
{...}
public void direction(Coordinate coordinate)
{...}
public void direction(int long, int lat)
{...}
First class collections
4
class Board {
public String board() {
StringBuilder buf = new StringBuilder();
collectRows(buf);
return buf.toString();
}
private void collectRows(StringBuilder buf) {
for (int i = 0; i < 10; i++) {
collectRow(buf, i);
}
}
private void collectRow(StringBuilder buf, int row) {
for (int i = 0; i < 10; i++) {
buf.append(data[row][i]);
}
buf.append("n");
}
}
One dot per line
5
class Board {
public String boardRepresentation() {
StringBuilder buf = new StringBuilder();
for (Location loc : squares()) {
buf.append(loc.current.representation.substring(0, 1));
}
return buf.toString();
}
}
class Board {
public String boardRepresentation() {
StringBuilder buf = new StringBuilder();
for (Location loc : squares()) {
buf.append(loc.current.representation.substring(0, 1));
}
return buf.toString();
}
}
class Board {
public String boardRepresentation() {
StringBuilder buf = new StringBuilder();
for (Location location : squares()) {
location.addTo(buf);
}
return buf.toString();
}
}
Don’t abbreviate
6
public void Coordinate(int long, int lat){...}
public void Coordinate(int longitude, int latitude){...}
public void Coord(int long, int lat){...}
Keep all classes less
than 50 lines.
7
No classes with more
than two instance
variables.
8
public void direction(Coordinate coordinate, ...) {...}
public void direction(int long, int lat, string name){...}
No getters or setters.
9
// Game
private int score;
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
// Usage
game.setScore(game.getScore() + ENEMY_DESTROYED_SCORE);
// Game
private int score;
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
// Usage
game.setScore(game.getScore() + ENEMY_DESTROYED_SCORE);
// Game
public void addScore(int delta) {
score += delta;
}
// Usage
game.addScore(ENEMY_DESTROYED_SCORE);
9 rules
1. One level of indentation per method.
2. Don't use ELSE
3. Wrap all primitives and Strings in classes.
4. First class collections.
5. One dot per line.
6. Don't abbreviate.
7. Keep all classes less than 50 lines.
8. No classes with more than two instance variables.
9. No getters or setters.
38
References
39
References
OBJECT CALISTHENICS
http://williamdurand.fr/2013/06/03/object-calisthenics/
http://www.xpteam.com/jeff/writings/objectcalisthenics.rtf
http://www.pseale.com/linkblogging-object-calisthenics
https://www.youtube.com/watch?v=j6xSGqZoJVI
https://refactoring.com/catalog/extractMethod.html
https://www.youtube.com/watch?v=u-w4eULRrr0
https://pt.slideshare.net/guilhermeblanco/php-para-adultos-clean-code-e-object-calisthenics
41

More Related Content

What's hot

What's hot (6)

3. basic data structures(2)
3. basic data structures(2)3. basic data structures(2)
3. basic data structures(2)
 
Java Basics - Part2
Java Basics - Part2Java Basics - Part2
Java Basics - Part2
 
3D + MongoDB = 3D Repo
3D + MongoDB = 3D Repo3D + MongoDB = 3D Repo
3D + MongoDB = 3D Repo
 
Sample Paper 2 Class XI (Computer Science)
Sample Paper 2 Class XI (Computer Science)Sample Paper 2 Class XI (Computer Science)
Sample Paper 2 Class XI (Computer Science)
 
Strctures,strings,pointers
Strctures,strings,pointersStrctures,strings,pointers
Strctures,strings,pointers
 
Bai giaigk
Bai giaigkBai giaigk
Bai giaigk
 

Similar to Object calisthenics

import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
ADITIEYEWEAR
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
rajudasraju
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
Paulo Morgado
 
Boost.Interfaces
Boost.InterfacesBoost.Interfaces
Boost.Interfaces
melpon
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 

Similar to Object calisthenics (20)

Object calisthenics
Object calisthenicsObject calisthenics
Object calisthenics
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em Go
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 features
 
662305 09
662305 09662305 09
662305 09
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 
Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия Swift
 
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike MullerShared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
 
Boost.Interfaces
Boost.InterfacesBoost.Interfaces
Boost.Interfaces
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 

More from Thamara Hessel

More from Thamara Hessel (9)

Liderar e ser liderado(a) - o que você precisa saber sobre liderança técnica
Liderar e ser liderado(a) - o que você precisa saber sobre liderança técnicaLiderar e ser liderado(a) - o que você precisa saber sobre liderança técnica
Liderar e ser liderado(a) - o que você precisa saber sobre liderança técnica
 
What do software engineers do
What do software engineers do What do software engineers do
What do software engineers do
 
Arquitetura e qualidade de codigo
Arquitetura e qualidade de codigoArquitetura e qualidade de codigo
Arquitetura e qualidade de codigo
 
Composer - tricks and tips
Composer - tricks and tipsComposer - tricks and tips
Composer - tricks and tips
 
Git style best practices - OLX
Git style best practices - OLXGit style best practices - OLX
Git style best practices - OLX
 
Code review Effective - kwan
Code review  Effective - kwanCode review  Effective - kwan
Code review Effective - kwan
 
Git - Saia do Básico!
Git - Saia do Básico!Git - Saia do Básico!
Git - Saia do Básico!
 
Qualidade de código
Qualidade de códigoQualidade de código
Qualidade de código
 
Refactoring sem complicação!
Refactoring sem complicação!Refactoring sem complicação!
Refactoring sem complicação!
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
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...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 

Object calisthenics