SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
Object
Calisthenics
em 10 minutos
@jucycabrera
Object Calisthenics
● apresentado em 2008 por Jeff Bay
● Conjunto de regras no código orientado a
objetos, que se concentra em
manutenibilidade, legibilidade, testabilidade e
na facilidade de compreensão.
1
Um nível de
indentação por
método
2
Não use ELSE
Você pode substituir o else
por early return
3
Encapsule tipos
primitivos e
strings
se tiver um comportamento
class User
{
protected $email;
public function create(string $email){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
throw new Exception('Invalid email address');
}
// ...
}
}
class User
{
// ...
public function create(Email $email){
// ...
}
}
class Email
{
protected $email;
public function __construct(string $email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address');
}
$this->email = $email;
}
}
antes
depois
4
Um -> por linha
● Lei de Demeter: fale apenas
com seus amigos
● Código legível
class User
{
public function listUsers()
{
return $this->email->search('*')->orderBy('DESC');
}
}
5
Não abrevie
● Use nomes claros
● Única responsabilidade
6
Mantenha as
classes
pequenas
● 200 linhas
● 10 métodos
● 15 - 20 linhas por método
●
7
Limite a
quantidade de
variaveis de
instancia da
classe
class UserData
{
protected $address;
protected $county;
protected $city;
protected $zipcode;
protected $phone;
}
class UserData
{
/**
* @var Address
*/
protected $address;
protected $phone;
}
class Address
{
protected $address;
protected $county;
protected $city;
protected $zipcode;
}
antes
depois
no maximo 5
8
Use Collections
● Uma única propriedade
● interfaces SPL
● Filtros, ordenação etc
9
Não use getters
e setters
Mais informações
● Livro The Thoughtworks Anthology
● Slides da palestra do Guilherme Blanco
https://www.slideshare.net/guilhermeblanco/php-para-adultos-clean-code-e-o
bject-calisthenics
● Slides da palestra do Alessandro Feitoza
https://slides.com/alessandrofeitoza/voce-teria-1-minuto-para-falar-de-object
-calisthenics#/
Obrigada
@jucycabrera

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Objetos Calistenia em 10 minutos: regras para código limpo e legível

  • 2. Object Calisthenics ● apresentado em 2008 por Jeff Bay ● Conjunto de regras no código orientado a objetos, que se concentra em manutenibilidade, legibilidade, testabilidade e na facilidade de compreensão.
  • 4. 2 Não use ELSE Você pode substituir o else por early return
  • 5. 3 Encapsule tipos primitivos e strings se tiver um comportamento class User { protected $email; public function create(string $email){ if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ throw new Exception('Invalid email address'); } // ... } } class User { // ... public function create(Email $email){ // ... } } class Email { protected $email; public function __construct(string $email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address'); } $this->email = $email; } } antes depois
  • 6. 4 Um -> por linha ● Lei de Demeter: fale apenas com seus amigos ● Código legível class User { public function listUsers() { return $this->email->search('*')->orderBy('DESC'); } }
  • 7. 5 Não abrevie ● Use nomes claros ● Única responsabilidade
  • 8. 6 Mantenha as classes pequenas ● 200 linhas ● 10 métodos ● 15 - 20 linhas por método ●
  • 9. 7 Limite a quantidade de variaveis de instancia da classe class UserData { protected $address; protected $county; protected $city; protected $zipcode; protected $phone; } class UserData { /** * @var Address */ protected $address; protected $phone; } class Address { protected $address; protected $county; protected $city; protected $zipcode; } antes depois no maximo 5
  • 10. 8 Use Collections ● Uma única propriedade ● interfaces SPL ● Filtros, ordenação etc
  • 12.
  • 13. Mais informações ● Livro The Thoughtworks Anthology ● Slides da palestra do Guilherme Blanco https://www.slideshare.net/guilhermeblanco/php-para-adultos-clean-code-e-o bject-calisthenics ● Slides da palestra do Alessandro Feitoza https://slides.com/alessandrofeitoza/voce-teria-1-minuto-para-falar-de-object -calisthenics#/