object calisthenics for magento - magetitans usa 2017

109
CLEAN CODE, OBJECT CALISTHENICS & BEST PRACTICES - IT'S TEXAS, BABY! by #MageTitansUSA @mhgontijo

Upload: matheus-gontijo-mba

Post on 24-Jan-2018

392 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Object Calisthenics for Magento - MageTitans USA 2017

CLEAN CODE, OBJECT CALISTHENICS

& BEST PRACTICES  -  IT'S TEXAS, BABY!

by

#MageTitansUSA

@mhgontijo

Page 2: Object Calisthenics for Magento - MageTitans USA 2017

Matheus GontijoY-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 platformSpeaker and attendee to conferences all over the world

on twitter - ping me there   ;- D

Crimson Agility

MCD & MCD+@mhgontijo

Page 3: Object Calisthenics for Magento - MageTitans USA 2017

What KILLS So�ware Developers so badly?

Page 4: Object Calisthenics for Magento - MageTitans USA 2017
Page 5: Object Calisthenics for Magento - MageTitans USA 2017
Page 6: Object Calisthenics for Magento - MageTitans USA 2017

uhmm... there are  PLENTY  some guys still writing code

in our community like that...

Page 7: Object Calisthenics for Magento - MageTitans USA 2017

Poorly written code equals... 💩💩💩

countless hours of energy being drainedHUGE amount of time wasted === MONEY wasted...it's unpleseant... it discourages the entire team to workwith that mess

Page 8: Object Calisthenics for Magento - MageTitans USA 2017

How can we improve our code?

EXERCISING!

Page 9: Object Calisthenics for Magento - MageTitans USA 2017
Page 10: Object Calisthenics for Magento - MageTitans USA 2017
Page 11: Object Calisthenics for Magento - MageTitans USA 2017
Page 12: Object Calisthenics for Magento - MageTitans USA 2017

Object Calisthenics!btw has anybody ever heard about it?

Page 13: Object Calisthenics for Magento - MageTitans USA 2017

Cal • is • then • ics/ˌkaləsˈTHeniks/

Greek word that means gymnastic exercises.

Page 14: Object Calisthenics for Magento - MageTitans USA 2017

The concept was created by Jeff Bay in hisbook The ThoughtWorks Anthology

A collection of exercises to focus onmaintainability, readability, testability, and

comprehensibility of your code.

Page 15: Object Calisthenics for Magento - MageTitans USA 2017

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

Page 16: Object Calisthenics for Magento - MageTitans USA 2017

THAT SHOULDN'T BE EXTREMELY...

COMPLEX!!!

Page 17: Object Calisthenics for Magento - MageTitans USA 2017

9 simple rules to write code...

WAY BETTER ✌

Page 18: Object Calisthenics for Magento - MageTitans USA 2017

9 simple rules advices to write code...

WAY BETTER ✌

Page 19: Object Calisthenics for Magento - MageTitans USA 2017

BUT don't follow them blindly....

It's NOT binary: RIGHT or WRONGThere is not one-size-fits-allNo Silver BulletDon'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...

Page 20: Object Calisthenics for Magento - MageTitans USA 2017

30 minutes onlyjust going through quickly    ; )

the examples are very simple just for the sake of easyunderstanding

Page 21: Object Calisthenics for Magento - MageTitans USA 2017

#01 ADVICEOnly One Level Of Indentation Per

Method

Page 22: Object Calisthenics for Magento - MageTitans USA 2017
Page 23: Object Calisthenics for Magento - MageTitans USA 2017
Page 24: Object Calisthenics for Magento - MageTitans USA 2017

Example:Let's say you are creating a

process to update description

Page 25: Object Calisthenics for Magento - MageTitans USA 2017
Page 26: Object Calisthenics for Magento - MageTitans USA 2017
Page 27: Object Calisthenics for Magento - MageTitans USA 2017
Page 28: Object Calisthenics for Magento - MageTitans USA 2017
Page 29: Object Calisthenics for Magento - MageTitans USA 2017
Page 30: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Easy to understand!Single Responsibility Principle ("S" in S.O.L.I.D.)Encourages Re-use

Page 31: Object Calisthenics for Magento - MageTitans USA 2017

#02 ADVICEDon't Use The ELSE Keyword

Page 32: Object Calisthenics for Magento - MageTitans USA 2017

Example:Let's create a class in order to

subscribe emails to a newslettergroup called VIP.

Page 33: Object Calisthenics for Magento - MageTitans USA 2017

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 assignedto VIP Group also.

Page 34: Object Calisthenics for Magento - MageTitans USA 2017
Page 35: Object Calisthenics for Magento - MageTitans USA 2017
Page 36: Object Calisthenics for Magento - MageTitans USA 2017
Page 37: Object Calisthenics for Magento - MageTitans USA 2017
Page 38: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Decreases Cyclomatic ComplexityPrevents Code DuplicationLegibility (Single Path)

Page 39: Object Calisthenics for Magento - MageTitans USA 2017
Page 40: Object Calisthenics for Magento - MageTitans USA 2017
Page 41: Object Calisthenics for Magento - MageTitans USA 2017
Page 42: Object Calisthenics for Magento - MageTitans USA 2017
Page 43: Object Calisthenics for Magento - MageTitans USA 2017
Page 44: Object Calisthenics for Magento - MageTitans USA 2017
Page 45: Object Calisthenics for Magento - MageTitans USA 2017

#03 ADVICEWrap All Primitives Types And Strings,

If It Has Behavior

Page 46: Object Calisthenics for Magento - MageTitans USA 2017

When?When your type needs validations, business rules or

behaviors

EmailZipcodePhone NumberIP AddressIBAN, ISBNURLList of status as Pending, Denied, Approved...

Page 47: Object Calisthenics for Magento - MageTitans USA 2017

Example:Let's say you are creating a class to

send emails.

Page 48: Object Calisthenics for Magento - MageTitans USA 2017
Page 49: Object Calisthenics for Magento - MageTitans USA 2017
Page 50: Object Calisthenics for Magento - MageTitans USA 2017
Page 51: Object Calisthenics for Magento - MageTitans USA 2017
Page 52: Object Calisthenics for Magento - MageTitans USA 2017
Page 53: Object Calisthenics for Magento - MageTitans USA 2017
Page 54: Object Calisthenics for Magento - MageTitans USA 2017
Page 55: Object Calisthenics for Magento - MageTitans USA 2017
Page 56: Object Calisthenics for Magento - MageTitans USA 2017
Page 57: Object Calisthenics for Magento - MageTitans USA 2017
Page 58: Object Calisthenics for Magento - MageTitans USA 2017
Page 59: Object Calisthenics for Magento - MageTitans USA 2017
Page 60: Object Calisthenics for Magento - MageTitans USA 2017
Page 61: Object Calisthenics for Magento - MageTitans USA 2017
Page 62: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Type HintingEncapsulation of business rulesPrevents Code DuplicationImplements Value Objects of DDD

Page 63: Object Calisthenics for Magento - MageTitans USA 2017

#04 ADVICEFirst Class Collections

Page 64: Object Calisthenics for Magento - MageTitans USA 2017
Page 65: Object Calisthenics for Magento - MageTitans USA 2017
Page 66: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Single Responsibility Principle ("S" in S.O.L.I.D.)Implements SPL InterfacesClass for Filtering, Mapping, Ordering & other

Page 67: Object Calisthenics for Magento - MageTitans USA 2017

#05 ADVICEOne Dot Per Line

Page 68: Object Calisthenics for Magento - MageTitans USA 2017

#05 ADVICEOne Dot "->" Per Line

Page 69: Object Calisthenics for Magento - MageTitans USA 2017
Page 70: Object Calisthenics for Magento - MageTitans USA 2017
Page 71: Object Calisthenics for Magento - MageTitans USA 2017
Page 72: Object Calisthenics for Magento - MageTitans USA 2017
Page 73: Object Calisthenics for Magento - MageTitans USA 2017
Page 74: Object Calisthenics for Magento - MageTitans USA 2017

Law of Demeter

Page 75: Object Calisthenics for Magento - MageTitans USA 2017

*** EXCEPT ***for Fluent Interfaces/Method Chainingsince it was designed to work that way

Page 76: Object Calisthenics for Magento - MageTitans USA 2017
Page 77: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Law of DemeterLegibility

Page 78: Object Calisthenics for Magento - MageTitans USA 2017

#06 ADVICEDon't Abbreviate

Page 79: Object Calisthenics for Magento - MageTitans USA 2017
Page 80: Object Calisthenics for Magento - MageTitans USA 2017
Page 81: Object Calisthenics for Magento - MageTitans USA 2017
Page 82: Object Calisthenics for Magento - MageTitans USA 2017
Page 83: Object Calisthenics for Magento - MageTitans USA 2017
Page 84: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:LegibilityMaintainability

Page 85: Object Calisthenics for Magento - MageTitans USA 2017

#07 ADVICEKeep All Entities Small

Page 86: Object Calisthenics for Magento - MageTitans USA 2017
Page 87: Object Calisthenics for Magento - MageTitans USA 2017

Long files are hard to read... so...

No methods with over 20 linesNo namespaces over 15 filesClass with 200 lines maximum

Very challenging!

Page 88: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:LegibilityMaintainability

Page 89: Object Calisthenics for Magento - MageTitans USA 2017

#08 ADVICENo Classes With More Than Two Five

Instance Variables

Page 90: Object Calisthenics for Magento - MageTitans USA 2017
Page 91: Object Calisthenics for Magento - MageTitans USA 2017

Benefits:Higher CohesionLower CouplingShorter Dependency listMaintainability

Page 92: Object Calisthenics for Magento - MageTitans USA 2017

#09 ADVICENo Getters and Setters!

just my favorite ❤ ❤ ❤

Page 93: Object Calisthenics for Magento - MageTitans USA 2017

Example:Let's create a approval process for

comments.

Page 94: Object Calisthenics for Magento - MageTitans USA 2017

It's very important to the system totrack the following:

Previous StatusNew StatusUser that made the changeDate

Page 95: Object Calisthenics for Magento - MageTitans USA 2017

John is creating the a new approval record...

Status: PENDING

Page 96: Object Calisthenics for Magento - MageTitans USA 2017

Mary is creating the a new approval record...

Status: PENDING ---> FIX_TYPO

Page 97: Object Calisthenics for Magento - MageTitans USA 2017

David is creating the a new approval record...

Status: FIX_TYPO ---> APPROVED

Page 98: Object Calisthenics for Magento - MageTitans USA 2017
Page 99: Object Calisthenics for Magento - MageTitans USA 2017
Page 100: Object Calisthenics for Magento - MageTitans USA 2017

h4ck3r is creating the a new approval record...

Status: APPROVED ---> APPROVED

Page 101: Object Calisthenics for Magento - MageTitans USA 2017

h4ck3r is creating the a new approval record...

Status: DENIED ---> DENIED

Page 102: Object Calisthenics for Magento - MageTitans USA 2017

Solution?ENCAPSULATION!

We have to HIDE business logic!

BTW it's not something new... it's just encapsulation... basic concept of OOP

Page 103: Object Calisthenics for Magento - MageTitans USA 2017

Now nobody will no longer be able to mess up the process    ;- D

Page 104: Object Calisthenics for Magento - MageTitans USA 2017
Page 105: Object Calisthenics for Magento - MageTitans USA 2017

That's it!

✌ ✌ ✌ ✌

Page 106: Object Calisthenics for Magento - MageTitans USA 2017

Recap1. Only One Level Of Indentation Per Method2. Don't Use The ELSE Keyword3. Wrap All Primitives Types And Strings, If It Has Behavior4. First Class Collections5. One Dot "->" Per Line6. Don't Abbreviate7. Keep All Entities Small8. No Classes With More Than Two Five Instance Variables9. No Getters and Setters!

Page 107: Object Calisthenics for Magento - MageTitans USA 2017

and the most important thing is...

Page 108: Object Calisthenics for Magento - MageTitans USA 2017

You should giveObject Calisthenics a try!

Take a month and see how good that is!

Page 109: Object Calisthenics for Magento - MageTitans USA 2017

Questions?@mhgontijo