SlideShare a Scribd company logo
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

More Related Content

Object Calisthenics for Magento - MageTitans USA 2017