object calisthenics for magento - magetitans usa 2017

Post on 24-Jan-2018

392 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CLEAN CODE, OBJECT CALISTHENICS

& BEST PRACTICES  -  IT'S TEXAS, BABY!

by

#MageTitansUSA

@mhgontijo

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

What KILLS So�ware Developers so badly?

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

in our community like that...

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

How can we improve our code?

EXERCISING!

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 hisbook The ThoughtWorks Anthology

A collection of exercises to focus onmaintainability, 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 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...

30 minutes onlyjust going through quickly    ; )

the examples are very simple just for the sake of easyunderstanding

#01 ADVICEOnly One Level Of Indentation Per

Method

Example:Let's say you are creating a

process to update description

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

#02 ADVICEDon't Use The ELSE Keyword

Example:Let's create a class in order to

subscribe emails to a newslettergroup 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 assignedto VIP Group also.

Benefits:Decreases Cyclomatic ComplexityPrevents Code DuplicationLegibility (Single Path)

#03 ADVICEWrap All Primitives Types And Strings,

If It Has Behavior

When?When your type needs validations, business rules or

behaviors

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

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

send emails.

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

#04 ADVICEFirst Class Collections

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

#05 ADVICEOne Dot Per Line

#05 ADVICEOne Dot "->" Per Line

Law of Demeter

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

Benefits:Law of DemeterLegibility

#06 ADVICEDon't Abbreviate

Benefits:LegibilityMaintainability

#07 ADVICEKeep All Entities Small

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

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

Very challenging!

Benefits:LegibilityMaintainability

#08 ADVICENo Classes With More Than Two Five

Instance Variables

Benefits:Higher CohesionLower CouplingShorter Dependency listMaintainability

#09 ADVICENo Getters and Setters!

just my favorite ❤ ❤ ❤

Example:Let's create a approval process for

comments.

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

Previous StatusNew StatusUser that made the changeDate

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

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

That's it!

✌ ✌ ✌ ✌

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!

and the most important thing is...

You should giveObject Calisthenics a try!

Take a month and see how good that is!

Questions?@mhgontijo

top related