Object Calisthenics for Magento - MageTitans USA 2017

Matheus Gontijo, MBA
Matheus Gontijo, MBASoftware Engineer at Crimson Agility
CLEAN CODE,
OBJECT CALISTHENICS
& BEST PRACTICES
  -  IT'S TEXAS, BABY!
by
#MageTitansUSA
@mhgontijo
Matheus Gontijo
Y-E-E-A-A-H-H, we have a #Brazilian in the house, folks!
So ware Engineer at
Over 6 years of experience with Magento platform
Speaker and attendee to conferences all over the world
on twitter - ping me there   ;- D
Crimson Agility
MCD & MCD+
@mhgontijo
What KILLS So ware Developers
so badly?
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
uhmm... there are
 PLENTY  some guys still writing code
in our community like that...
Poorly written code equals...
💩💩💩
countless hours of energy being drained
HUGE amount of time wasted === MONEY wasted
...it's unpleseant... it discourages the entire team to work
with that mess
How can we improve our code?
EXERCISING!
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics!
btw has anybody ever heard about it?
Cal • is • then • ics
/ˌkaləsˈTHeniks/
Greek word that means
gymnastic exercises.
The concept was created by Jeff Bay in his
book The ThoughtWorks Anthology
A collection of exercises to focus on
maintainability, readability, testability, and
comprehensibility of your code.
PHP Community?
Rafael Dohms -
Keynode Speaker, Evangelist, and founder of PHPSP &
PHPAmsterdam... which are awesome!
Guilherme Blanco -
Core Committer at Doctrine, Symfony and Zend Framework
@rdohms
@guilhermeblanco
THAT SHOULDN'T BE EXTREMELY...
COMPLEX!!!
9 simple rules
to write code...
WAY BETTER ✌
9 simple rules advices
to write code...
WAY BETTER ✌
BUT don't follow them blindly....
It's NOT binary: RIGHT or WRONG
There is not one-size-fits-all
No Silver Bullet
Don't take what I'm saying as absolute truth   :- P
the main goal is to challenge yourself...
to make you RE-think the way you code...
30 minutes only
just going through quickly    ; )
the examples are very simple just for the sake of easy
understanding
#01 ADVICE
Only One Level Of Indentation Per
Method
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Example:
Let's say you are creating a
process to update description
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Easy to understand!
Single Responsibility Principle ("S" in S.O.L.I.D.)
Encourages Re-use
#02 ADVICE
Don't Use The ELSE Keyword
Example:
Let's create a class in order to
subscribe emails to a newsletter
group called VIP.
Rules:
It has to be a valid email.
The email can not be registered again, if it already is.
The email has to have a customer assigned the same email.
The customer assigned to that email needs to be assigned
to VIP Group also.
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Decreases Cyclomatic Complexity
Prevents Code Duplication
Legibility (Single Path)
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
#03 ADVICE
Wrap All Primitives Types And Strings,
If It Has Behavior
When?
When your type needs validations, business rules or
behaviors
Email
Zipcode
Phone Number
IP Address
IBAN, ISBN
URL
List of status as Pending, Denied, Approved
...
Example:
Let's say you are creating a class to
send emails.
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Type Hinting
Encapsulation of business rules
Prevents Code Duplication
Implements Value Objects of DDD
#04 ADVICE
First Class Collections
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Single Responsibility Principle ("S" in S.O.L.I.D.)
Implements SPL Interfaces
Class for Filtering, Mapping, Ordering & other
#05 ADVICE
One Dot Per Line
#05 ADVICE
One Dot "->" Per Line
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Law of Demeter
*** EXCEPT ***
for Fluent Interfaces/Method Chaining
since it was designed to work that way
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Law of Demeter
Legibility
#06 ADVICE
Don't Abbreviate
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Legibility
Maintainability
#07 ADVICE
Keep All Entities Small
Object Calisthenics for Magento - MageTitans USA 2017
Long files are hard to read... so...
No methods with over 20 lines
No namespaces over 15 files
Class with 200 lines maximum
Very challenging!
Benefits:
Legibility
Maintainability
#08 ADVICE
No Classes With More Than Two Five
Instance Variables
Object Calisthenics for Magento - MageTitans USA 2017
Benefits:
Higher Cohesion
Lower Coupling
Shorter Dependency list
Maintainability
#09 ADVICE
No Getters and Setters!
just my favorite ❤ ❤ ❤
Example:
Let's create a approval process for
comments.
It's very important to the system to
track the following:
Previous Status
New Status
User that made the change
Date
John is creating the a new approval record...
Status: PENDING
Mary is creating the a new approval record...
Status: PENDING ---> FIX_TYPO
David is creating the a new approval record...
Status: FIX_TYPO ---> APPROVED
Object Calisthenics for Magento - MageTitans USA 2017
Object Calisthenics for Magento - MageTitans USA 2017
h4ck3r is creating the a new approval record...
Status: APPROVED ---> APPROVED
h4ck3r is creating the a new approval record...
Status: DENIED ---> DENIED
Solution?
ENCAPSULATION!
We have to HIDE business logic!
BTW it's not something new... it's just encapsulation... basic concept of OOP
Now nobody will no longer be able
to mess up the process    ;- D
Object Calisthenics for Magento - MageTitans USA 2017
That's it!
✌ ✌ ✌ ✌
Recap
1. Only One Level Of Indentation Per Method
2. Don't Use The ELSE Keyword
3. Wrap All Primitives Types And Strings, If It Has Behavior
4. First Class Collections
5. One Dot "->" Per Line
6. Don't Abbreviate
7. Keep All Entities Small
8. No Classes With More Than Two Five Instance Variables
9. No Getters and Setters!
and the most important thing is...
You should give
Object Calisthenics a try!
Take a month and see how good that is!
Questions?
@mhgontijo
1 of 109

Recommended

Object Calisthenics for Magento - Meet Magento New York 2017 by
Object Calisthenics for Magento - Meet Magento New York 2017Object Calisthenics for Magento - Meet Magento New York 2017
Object Calisthenics for Magento - Meet Magento New York 2017Matheus Gontijo, MBA
825 views105 slides
Refactor your Specs - 2017 Edition by
Refactor your Specs - 2017 EditionRefactor your Specs - 2017 Edition
Refactor your Specs - 2017 EditionCyrille Martraire
780 views192 slides
TDD and Simple Design Workshop - Session 1 - March 2019 by
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
811 views51 slides
Agency War Stories by
Agency War StoriesAgency War Stories
Agency War StoriesCode Computerlove
356 views135 slides
How to crack Admin and Advanced Admin by
How to crack Admin and Advanced AdminHow to crack Admin and Advanced Admin
How to crack Admin and Advanced AdminKadharBashaJ
124 views41 slides
Code Quality Makes Your Job Easier by
Code Quality Makes Your Job EasierCode Quality Makes Your Job Easier
Code Quality Makes Your Job EasierTonya Mork
472 views63 slides

More Related Content

Similar to Object Calisthenics for Magento - MageTitans USA 2017

So You Want to Rewrite That... by
So You Want to Rewrite That...So You Want to Rewrite That...
So You Want to Rewrite That...Camille Fournier
1.3K views54 slides
Test driven-development by
Test driven-developmentTest driven-development
Test driven-developmentDavid Paluy
737 views29 slides
A geek sifts through the bullshit by
A geek sifts through the bullshitA geek sifts through the bullshit
A geek sifts through the bullshitWebstock
2.1K views96 slides
Change How You Do Product - by Tal Ben-Simon (ProductX 2018) by
Change How You Do Product - by Tal Ben-Simon (ProductX 2018)Change How You Do Product - by Tal Ben-Simon (ProductX 2018)
Change How You Do Product - by Tal Ben-Simon (ProductX 2018)Tal Ben-Simon
260 views59 slides
2010 03 09 the lean startup - gdc by
2010 03 09 the lean startup - gdc2010 03 09 the lean startup - gdc
2010 03 09 the lean startup - gdcEric Ries
12.1K views51 slides
Building Startups and Minimum Viable Products (NDC2013) by
Building Startups and Minimum Viable Products (NDC2013)Building Startups and Minimum Viable Products (NDC2013)
Building Startups and Minimum Viable Products (NDC2013)Ben Hall
1.5K views94 slides

Similar to Object Calisthenics for Magento - MageTitans USA 2017(20)

Test driven-development by David Paluy
Test driven-developmentTest driven-development
Test driven-development
David Paluy737 views
A geek sifts through the bullshit by Webstock
A geek sifts through the bullshitA geek sifts through the bullshit
A geek sifts through the bullshit
Webstock2.1K views
Change How You Do Product - by Tal Ben-Simon (ProductX 2018) by Tal Ben-Simon
Change How You Do Product - by Tal Ben-Simon (ProductX 2018)Change How You Do Product - by Tal Ben-Simon (ProductX 2018)
Change How You Do Product - by Tal Ben-Simon (ProductX 2018)
Tal Ben-Simon260 views
2010 03 09 the lean startup - gdc by Eric Ries
2010 03 09 the lean startup - gdc2010 03 09 the lean startup - gdc
2010 03 09 the lean startup - gdc
Eric Ries12.1K views
Building Startups and Minimum Viable Products (NDC2013) by Ben Hall
Building Startups and Minimum Viable Products (NDC2013)Building Startups and Minimum Viable Products (NDC2013)
Building Startups and Minimum Viable Products (NDC2013)
Ben Hall1.5K views
Workshop: Behavior Driven Development - Deliver value by Naveen Kumar Singh by Agile ME
Workshop: Behavior Driven Development - Deliver value by Naveen Kumar SinghWorkshop: Behavior Driven Development - Deliver value by Naveen Kumar Singh
Workshop: Behavior Driven Development - Deliver value by Naveen Kumar Singh
Agile ME429 views
Joel Klettke - Engage 2020 (Positiong, Branding, and Conversions) by Joel Klettke
Joel Klettke - Engage 2020 (Positiong, Branding, and Conversions)Joel Klettke - Engage 2020 (Positiong, Branding, and Conversions)
Joel Klettke - Engage 2020 (Positiong, Branding, and Conversions)
Joel Klettke173 views
The Art & Science of a Successful Cold Email Strategy by Datanyze
The Art & Science of a Successful Cold Email StrategyThe Art & Science of a Successful Cold Email Strategy
The Art & Science of a Successful Cold Email Strategy
Datanyze 1.6K views
Who owns the AV department - Creating an AV Master Plan by Harald Steindl
Who owns the AV department - Creating an AV Master PlanWho owns the AV department - Creating an AV Master Plan
Who owns the AV department - Creating an AV Master Plan
Harald Steindl1.9K views
Who owns the AV department - Creating an AV Master Plan by Harald Unger
Who owns the AV department - Creating an AV Master PlanWho owns the AV department - Creating an AV Master Plan
Who owns the AV department - Creating an AV Master Plan
Harald Unger144 views
2010 02 19 the lean startup - webstock 2010 by Eric Ries
2010 02 19 the lean startup - webstock 20102010 02 19 the lean startup - webstock 2010
2010 02 19 the lean startup - webstock 2010
Eric Ries18.1K views
The art and science of a successful cold email strategy by Karen Gitlin
The art and science of a successful cold email strategyThe art and science of a successful cold email strategy
The art and science of a successful cold email strategy
Karen Gitlin172 views
Legis pactum building high performance teams by Miguel Pinto
Legis pactum   building high performance teamsLegis pactum   building high performance teams
Legis pactum building high performance teams
Miguel Pinto97 views
10 Tips on How to Pitch a VC (FOWA, London) by Dave McClure
10 Tips on How to Pitch a VC (FOWA, London)10 Tips on How to Pitch a VC (FOWA, London)
10 Tips on How to Pitch a VC (FOWA, London)
Dave McClure40.8K views

More from Matheus Gontijo, MBA

Soft-skills: impacto direto na sua jornada como dev by
Soft-skills: impacto direto na sua jornada como devSoft-skills: impacto direto na sua jornada como dev
Soft-skills: impacto direto na sua jornada como devMatheus Gontijo, MBA
254 views39 slides
Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus Gontijo by
Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus GontijoGit, essa cara é fera! FTSL 2013 - UTFPR - por Matheus Gontijo
Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus GontijoMatheus Gontijo, MBA
981 views37 slides
Magento: um gigante no mundo do eCommerce! SOLISC 2013 by
Magento: um gigante no mundo do eCommerce! SOLISC 2013Magento: um gigante no mundo do eCommerce! SOLISC 2013
Magento: um gigante no mundo do eCommerce! SOLISC 2013Matheus Gontijo, MBA
1.6K views57 slides
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe... by
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...Matheus Gontijo, MBA
4.2K views112 slides
PHPDAY - PHP com excelência - Só para os fortes! por Matheus Gontijo by
PHPDAY - PHP com excelência - Só para os fortes! por Matheus GontijoPHPDAY - PHP com excelência - Só para os fortes! por Matheus Gontijo
PHPDAY - PHP com excelência - Só para os fortes! por Matheus GontijoMatheus Gontijo, MBA
828 views102 slides
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu... by
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...Matheus Gontijo, MBA
1.6K views44 slides

More from Matheus Gontijo, MBA(6)

Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus Gontijo by Matheus Gontijo, MBA
Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus GontijoGit, essa cara é fera! FTSL 2013 - UTFPR - por Matheus Gontijo
Git, essa cara é fera! FTSL 2013 - UTFPR - por Matheus Gontijo
Magento: um gigante no mundo do eCommerce! SOLISC 2013 by Matheus Gontijo, MBA
Magento: um gigante no mundo do eCommerce! SOLISC 2013Magento: um gigante no mundo do eCommerce! SOLISC 2013
Magento: um gigante no mundo do eCommerce! SOLISC 2013
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe... by Matheus Gontijo, MBA
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...
Desenvolvimento Magento: Criando temas para a realidade do Brasil - por Mathe...
PHPDAY - PHP com excelência - Só para os fortes! por Matheus Gontijo by Matheus Gontijo, MBA
PHPDAY - PHP com excelência - Só para os fortes! por Matheus GontijoPHPDAY - PHP com excelência - Só para os fortes! por Matheus Gontijo
PHPDAY - PHP com excelência - Só para os fortes! por Matheus Gontijo
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu... by Matheus Gontijo, MBA
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...
FLISOL 2013 - Magento eCommerce, uma plataforma de tirar o fôlego! por Matheu...

Recently uploaded

Zero to Automated in Under a Year by
Zero to Automated in Under a YearZero to Automated in Under a Year
Zero to Automated in Under a YearNetwork Automation Forum
15 views23 slides
Kyo - Functional Scala 2023.pdf by
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
400 views92 slides
Piloting & Scaling Successfully With Microsoft Viva by
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft VivaRichard Harbridge
12 views160 slides
The Forbidden VPN Secrets.pdf by
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdfMariam Shaba
20 views72 slides
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...Bernd Ruecker
40 views69 slides
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...The Digital Insurer
18 views52 slides

Recently uploaded(20)

Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
The Forbidden VPN Secrets.pdf by Mariam Shaba
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdf
Mariam Shaba20 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker40 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc11 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn22 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab21 views
Future of AR - Facebook Presentation by ssuserb54b561
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
ssuserb54b56115 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb14 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays11 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson92 views

Object Calisthenics for Magento - MageTitans USA 2017