SlideShare uma empresa Scribd logo
1 de 98
Baixar para ler offline
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Stories from
the Other Side
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Exploring OOP and
Functional Programming
(to become a better programmer)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Why am I interested in this?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Smart guys go FP (lambdalicious, phunkie)
The Little Schemer
Scala is the rage!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What is this, OOP + FP?
Where did these paradigms start?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
The term "Object Oriented Programming"
was coined by Alan Kay
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Original image by Marcin Wichary - Thanks!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Amongst many other achievements,
Alan Kay created Smalltalk
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
"OOP to me means only messaging,
local retention and protection
and hiding of state-process,
and extreme late-binding of all things"
It can be done in Smalltalk and in LISP."
~ Alan Kay
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
"It can be done in Smalltalk and in LISP."
wait... WHAT?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
OOP in LISP?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
((lambda (f)
(define pick
(lambda (n lat)
(cond
((zero? (sub1 n)) (car lat))
(else (pick (sub1 n) (cdr lat))))))
It's lists and recursion all the way down
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
How does OOP work with that?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What is OOP?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Is it about inheritance?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
"OOP to me means only messaging,
local retention and protection
and hiding of state-process,
and extreme late-binding of all things"
~ Alan Kay
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Kay doesn't mention inheritance.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Is OOP about classes?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Kay doesn't mention classes either.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
If OOP is not about
classes or inheritance,
what is it about?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Lets start with
Messaging
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
I guess that means method calls.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
In class based OOP,
the methods of a class define its
Interface.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Interface
==
Related Methods
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Quite similar to
related Functions
within a Namespace
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
So OOP is about grouping methods together?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
"... local retention and protection ..."
~ Alan Kay
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Oh yeah, objects have states.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What is a state?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
State in class based OOP:
class Foo
{
private static $class_state = 42;
private $instance_state = -0.2;
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
State in RL is more like
public function __construct(...)
{
$this->type = 'simple';
$this->customerSession = $customerSession;
$this->scopeConfig = $scopeConfig;
$this->counter = 0;
$this->id = $_REQUEST['id'];
$this->names = $db->getNames();
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What properties do these examples of
state have?
public function __construct(...)
{
$this->type = 'simple';
$this->customerSession = $customerSession;
$this->scopeConfig = $scopeConfig;
$this->counter = 0;
$this->id = $_REQUEST['id'];
$this->names = $db->getNames();
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
A literal
$this->type = 'simple';
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
A shared object
$this->customerSession = $customerSession;
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Literal zero
(but looks like that might change)
$this->counter = 0;
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Global state
$this->id = $_REQUEST['id'];
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Stuff from the DB
$this->names = $db->getNames();
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Can we partition those examples?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
1. State that is always the same
2. State that might change
(within the lifespan of the object)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
State that might change:
→ Counter
→ Session
→ Filesystem / DB / Internet
→ Globals
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Non-changing state
→ The Type ID
→ Application State
→ Configuration
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What can we do with
this information?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Pure
Functions!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Pure functions?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Don't cause any side effects
→ Given the same arguments
always return the same result
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
What are
side effects?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Changing state non-local to the
function scope
→ DB or Filesystem or Network access
→ Throwing Exceptions
→ Forking
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
No side effects
was one part.
What was the other?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Same Arguments
==>
Same Result
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
A function that always returns the same
result given the same arguments is called
Referentially Transparent
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Referentially Transparent Functions
must not use state that might change!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Why should we care?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Benefits
of Pure Functions!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Easier to think about
(reasonability)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Easier to break apart
(decomposability)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Easier to combine
(composability & reuseability)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Easier to show correctness
(testability)
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
→ Easier to parallelize
(threadability ;))
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Mkay.
But what about the
changing things?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
How can we deal with changing state?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We treat objects as snapshots of the
state of the world in one moment.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
The state of the world in that
moment will never change.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
If change occurs, it means now is a
different moment in time.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We can represent change in two ways:
→ by modifying the object
→ by creating a new instance
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Immutability
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
For example, what happens when an
admin logs in?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
$loggedInAdmin = $loggedOutAdmin->login();
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
public function login()
{
return new self($isLoggedIn = true);
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
But what if a method
needs the
"current instance"?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Either that object has to be recreated
with the current instance,
or the current instance has to be
passed as a method argument.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
OOP style
$authorizationManager->hasAccess($dashboard, $loggedInAdmin);
$authorizationManager->hasAccess($dashboard, $loggedOutAdmin);
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
In FP
$has_access($dashboard, $loggedInAdmin);
$has_access($dashboard, $loggedOutAdmin);
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
There are more
parameters in RL!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Okay, lets reduce the argument count by
adding properties to the object.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
$dashboardAuthorizationManager =
new class($authorizationManager, $dashboard) {
private $authorizationManager; private $page;
public function __construct($authorizationManager, $page)
{
$this->authorizationManager = $authorizationManager;
$this->page = $page;
}
public function hasAccess($admin)
{
return $this->authorizationManager
->hasAccess($this->page, $admin);
}
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Only one argument left:
$pageAuthorizationManager->hasAccess($loggedInAdmin);
$pageAuthorizationManager->hasAccess($loggedOutAdmin);
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
How might that look like in FP?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
$has_access_to_dashboard =
function ($admin) use ($dashboard, $has_access) {
return $has_access($dashboard, $admin);
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
$has_access_to_dashboard($loggedInAdmin);
$has_access_to_dashboard($loggedOutAdmin);
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Can we remove all
arguments?
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
$loggedInAdminDashboardAuthorization =
new class($dashboardAuthorizationManager, $loggedInAdmin)
{
private $pageAuthorizationManager; private $admin;
public function __construct($pageAuthorizationManager, $admin)
{
$this->pageAuthorizationManager = $pageAuthorizationManager;
$this->admin = $admin;
}
public function hasAccess()
{
return $this->pageAuthorizationManager
->hasAccess($this->admin);
}
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
In FP:
$has_logged_in_admin_access_to_dashboard =
function () use ($has_access_to_dashboard, $admin) {
return $has_access_to_dashboard($admin);
}
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We can bundle state
and functions
without classes!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We can bundle state
and process
without classes!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Look Ma,
OOP in PHP without
hands classes!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We have the choice:
→ pass state with method arguments
→ set state as object properties
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
The more volatile the object state,
the more often we need to
create new instances...
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
...if we want to have
Pure Functions.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Lesson from FP:
Distinguish between mutable and
immutable object properties.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
"Mutable stateful objects are the new
spaghetti code"
~ Rich Hickey
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Summary
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Concepts from the FP languages can be
useful in OO PHP.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We can benefit from designing our
OO code so it exhibits the properties of
pure functions.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
We barely scratched the surface.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Don't be afraid of FP's mathematical
pattern names. It's still just code.
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
Building these slides I
learned something about OOP.
I hope you found it interesting, too!
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
(if (has-communication? you vinai)
(communicate you vinai)
(enjoy you remaining-agenda))
MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Mais conteúdo relacionado

Destaque

Secure input and output handling - Meet Magento Romania 2016
Secure input and output handling - Meet Magento Romania 2016Secure input and output handling - Meet Magento Romania 2016
Secure input and output handling - Meet Magento Romania 2016Anna Völkl
 
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social Communicatio
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social CommunicatioPeter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social Communicatio
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social CommunicatioBusiness Development Institute
 
Tectonic Summit 2016: Preparing for Cloud Native
Tectonic Summit 2016: Preparing for Cloud Native Tectonic Summit 2016: Preparing for Cloud Native
Tectonic Summit 2016: Preparing for Cloud Native CoreOS
 
Cloud Solution Day 2016: Service Mesh for Kubernetes
Cloud Solution Day 2016: Service Mesh for KubernetesCloud Solution Day 2016: Service Mesh for Kubernetes
Cloud Solution Day 2016: Service Mesh for KubernetesAWS Vietnam Community
 
Resilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockResilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockDavid Schmitz
 
AWS Keynote 1 - AWS Enterprise Summit - Hong Kong
AWS Keynote 1 - AWS Enterprise Summit - Hong KongAWS Keynote 1 - AWS Enterprise Summit - Hong Kong
AWS Keynote 1 - AWS Enterprise Summit - Hong KongAmazon Web Services
 
Openstack in 10 mins
Openstack in 10 minsOpenstack in 10 mins
Openstack in 10 minsDawood M.S
 
Liberty release: Preliminary marketing materials & messages
Liberty release: Preliminary marketing materials & messagesLiberty release: Preliminary marketing materials & messages
Liberty release: Preliminary marketing materials & messagesOpenStack Foundation
 
OpenStack Summits 101: A Guide For Attendees
OpenStack Summits 101: A Guide For AttendeesOpenStack Summits 101: A Guide For Attendees
OpenStack Summits 101: A Guide For AttendeesOpenStack Foundation
 
CIECH - Financial results for 1H2016
CIECH - Financial results for 1H2016CIECH - Financial results for 1H2016
CIECH - Financial results for 1H2016CIECH Group IR
 

Destaque (14)

Apache ManifoldCF
Apache ManifoldCFApache ManifoldCF
Apache ManifoldCF
 
Secure input and output handling - Meet Magento Romania 2016
Secure input and output handling - Meet Magento Romania 2016Secure input and output handling - Meet Magento Romania 2016
Secure input and output handling - Meet Magento Romania 2016
 
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social Communicatio
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social CommunicatioPeter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social Communicatio
Peter Gannon Presentation - BDI 3/29/12 HCP Healthcare Social Communicatio
 
Tectonic Summit 2016: Preparing for Cloud Native
Tectonic Summit 2016: Preparing for Cloud Native Tectonic Summit 2016: Preparing for Cloud Native
Tectonic Summit 2016: Preparing for Cloud Native
 
Cloud Solution Day 2016: Service Mesh for Kubernetes
Cloud Solution Day 2016: Service Mesh for KubernetesCloud Solution Day 2016: Service Mesh for Kubernetes
Cloud Solution Day 2016: Service Mesh for Kubernetes
 
Resilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockResilience testing with Wiremock and Spock
Resilience testing with Wiremock and Spock
 
AWS Keynote 1 - AWS Enterprise Summit - Hong Kong
AWS Keynote 1 - AWS Enterprise Summit - Hong KongAWS Keynote 1 - AWS Enterprise Summit - Hong Kong
AWS Keynote 1 - AWS Enterprise Summit - Hong Kong
 
Gdeploy 2.0
Gdeploy 2.0Gdeploy 2.0
Gdeploy 2.0
 
SFScon16 - Michele Baldessari: "OpenStack – An introduction"
SFScon16 - Michele Baldessari: "OpenStack – An introduction"SFScon16 - Michele Baldessari: "OpenStack – An introduction"
SFScon16 - Michele Baldessari: "OpenStack – An introduction"
 
Openstack in 10 mins
Openstack in 10 minsOpenstack in 10 mins
Openstack in 10 mins
 
Liberty release: Preliminary marketing materials & messages
Liberty release: Preliminary marketing materials & messagesLiberty release: Preliminary marketing materials & messages
Liberty release: Preliminary marketing materials & messages
 
OpenStack Summits 101: A Guide For Attendees
OpenStack Summits 101: A Guide For AttendeesOpenStack Summits 101: A Guide For Attendees
OpenStack Summits 101: A Guide For Attendees
 
Infra for startup
Infra for startupInfra for startup
Infra for startup
 
CIECH - Financial results for 1H2016
CIECH - Financial results for 1H2016CIECH - Financial results for 1H2016
CIECH - Financial results for 1H2016
 

Mais de vinaikopp

Building Mage-OS - MageTitans 2023
Building Mage-OS - MageTitans 2023Building Mage-OS - MageTitans 2023
Building Mage-OS - MageTitans 2023vinaikopp
 
Hyvä: Compatibility Modules
Hyvä: Compatibility ModulesHyvä: Compatibility Modules
Hyvä: Compatibility Modulesvinaikopp
 
Hyvä from a developer perspective
Hyvä from a developer perspectiveHyvä from a developer perspective
Hyvä from a developer perspectivevinaikopp
 
Property Based Testing in PHP
Property Based Testing in PHPProperty Based Testing in PHP
Property Based Testing in PHPvinaikopp
 
Property based testing - MageTestFest 2019
Property based testing - MageTestFest 2019Property based testing - MageTestFest 2019
Property based testing - MageTestFest 2019vinaikopp
 
Becoming Certified - MageTitansMCR 2018
Becoming Certified - MageTitansMCR 2018Becoming Certified - MageTitansMCR 2018
Becoming Certified - MageTitansMCR 2018vinaikopp
 
SOS UiComponents
SOS UiComponentsSOS UiComponents
SOS UiComponentsvinaikopp
 
ClojureScript in Magento 2 - PHPUGMRN
ClojureScript in Magento 2 - PHPUGMRNClojureScript in Magento 2 - PHPUGMRN
ClojureScript in Magento 2 - PHPUGMRNvinaikopp
 
Magento 2 TDD Code Kata
Magento 2 TDD Code KataMagento 2 TDD Code Kata
Magento 2 TDD Code Katavinaikopp
 
Magento 2 TDD Code Kata Intro
Magento 2 TDD Code Kata IntroMagento 2 TDD Code Kata Intro
Magento 2 TDD Code Kata Introvinaikopp
 
Testing Magento 2
Testing Magento 2Testing Magento 2
Testing Magento 2vinaikopp
 
ClojureScript in Magento 2 - MageTitansMCR 2017
ClojureScript in Magento 2 - MageTitansMCR 2017ClojureScript in Magento 2 - MageTitansMCR 2017
ClojureScript in Magento 2 - MageTitansMCR 2017vinaikopp
 
Lizards & Pumpkins Catalog Replacement at mm17de
Lizards & Pumpkins Catalog Replacement at mm17deLizards & Pumpkins Catalog Replacement at mm17de
Lizards & Pumpkins Catalog Replacement at mm17devinaikopp
 
Writing Testable Code (for Magento 1 and 2) 2016 Romaina
Writing Testable Code (for Magento 1 and 2)  2016 RomainaWriting Testable Code (for Magento 1 and 2)  2016 Romaina
Writing Testable Code (for Magento 1 and 2) 2016 Romainavinaikopp
 
Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)vinaikopp
 
Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)vinaikopp
 
Getting your Hands Dirty Testing Magento 2 (at London Meetup)
Getting your Hands Dirty Testing Magento 2 (at London Meetup)Getting your Hands Dirty Testing Magento 2 (at London Meetup)
Getting your Hands Dirty Testing Magento 2 (at London Meetup)vinaikopp
 
Getting your hands dirty testing Magento 2 (at MageTitansIT)
Getting your hands dirty testing Magento 2 (at MageTitansIT)Getting your hands dirty testing Magento 2 (at MageTitansIT)
Getting your hands dirty testing Magento 2 (at MageTitansIT)vinaikopp
 
Architecture in-the-small-slides
Architecture in-the-small-slidesArchitecture in-the-small-slides
Architecture in-the-small-slidesvinaikopp
 
Modern Module Architecture
Modern Module ArchitectureModern Module Architecture
Modern Module Architecturevinaikopp
 

Mais de vinaikopp (20)

Building Mage-OS - MageTitans 2023
Building Mage-OS - MageTitans 2023Building Mage-OS - MageTitans 2023
Building Mage-OS - MageTitans 2023
 
Hyvä: Compatibility Modules
Hyvä: Compatibility ModulesHyvä: Compatibility Modules
Hyvä: Compatibility Modules
 
Hyvä from a developer perspective
Hyvä from a developer perspectiveHyvä from a developer perspective
Hyvä from a developer perspective
 
Property Based Testing in PHP
Property Based Testing in PHPProperty Based Testing in PHP
Property Based Testing in PHP
 
Property based testing - MageTestFest 2019
Property based testing - MageTestFest 2019Property based testing - MageTestFest 2019
Property based testing - MageTestFest 2019
 
Becoming Certified - MageTitansMCR 2018
Becoming Certified - MageTitansMCR 2018Becoming Certified - MageTitansMCR 2018
Becoming Certified - MageTitansMCR 2018
 
SOS UiComponents
SOS UiComponentsSOS UiComponents
SOS UiComponents
 
ClojureScript in Magento 2 - PHPUGMRN
ClojureScript in Magento 2 - PHPUGMRNClojureScript in Magento 2 - PHPUGMRN
ClojureScript in Magento 2 - PHPUGMRN
 
Magento 2 TDD Code Kata
Magento 2 TDD Code KataMagento 2 TDD Code Kata
Magento 2 TDD Code Kata
 
Magento 2 TDD Code Kata Intro
Magento 2 TDD Code Kata IntroMagento 2 TDD Code Kata Intro
Magento 2 TDD Code Kata Intro
 
Testing Magento 2
Testing Magento 2Testing Magento 2
Testing Magento 2
 
ClojureScript in Magento 2 - MageTitansMCR 2017
ClojureScript in Magento 2 - MageTitansMCR 2017ClojureScript in Magento 2 - MageTitansMCR 2017
ClojureScript in Magento 2 - MageTitansMCR 2017
 
Lizards & Pumpkins Catalog Replacement at mm17de
Lizards & Pumpkins Catalog Replacement at mm17deLizards & Pumpkins Catalog Replacement at mm17de
Lizards & Pumpkins Catalog Replacement at mm17de
 
Writing Testable Code (for Magento 1 and 2) 2016 Romaina
Writing Testable Code (for Magento 1 and 2)  2016 RomainaWriting Testable Code (for Magento 1 and 2)  2016 Romaina
Writing Testable Code (for Magento 1 and 2) 2016 Romaina
 
Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)
 
Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)
 
Getting your Hands Dirty Testing Magento 2 (at London Meetup)
Getting your Hands Dirty Testing Magento 2 (at London Meetup)Getting your Hands Dirty Testing Magento 2 (at London Meetup)
Getting your Hands Dirty Testing Magento 2 (at London Meetup)
 
Getting your hands dirty testing Magento 2 (at MageTitansIT)
Getting your hands dirty testing Magento 2 (at MageTitansIT)Getting your hands dirty testing Magento 2 (at MageTitansIT)
Getting your hands dirty testing Magento 2 (at MageTitansIT)
 
Architecture in-the-small-slides
Architecture in-the-small-slidesArchitecture in-the-small-slides
Architecture in-the-small-slides
 
Modern Module Architecture
Modern Module ArchitectureModern Module Architecture
Modern Module Architecture
 

Último

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Último (20)

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

Stories from the other side

  • 1. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 2. Stories from the Other Side MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 3. Exploring OOP and Functional Programming (to become a better programmer) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 4. Why am I interested in this? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 5. Smart guys go FP (lambdalicious, phunkie) The Little Schemer Scala is the rage! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 6. What is this, OOP + FP? Where did these paradigms start? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 7. The term "Object Oriented Programming" was coined by Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 8. Original image by Marcin Wichary - Thanks! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 9. Amongst many other achievements, Alan Kay created Smalltalk MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 10. "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things" It can be done in Smalltalk and in LISP." ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 11. "It can be done in Smalltalk and in LISP." wait... WHAT? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 12. OOP in LISP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 13. ((lambda (f) (define pick (lambda (n lat) (cond ((zero? (sub1 n)) (car lat)) (else (pick (sub1 n) (cdr lat)))))) It's lists and recursion all the way down MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 14. How does OOP work with that? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 15. What is OOP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 16. Is it about inheritance? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 17. "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things" ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 18. Kay doesn't mention inheritance. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 19. Is OOP about classes? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 20. Kay doesn't mention classes either. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 21. If OOP is not about classes or inheritance, what is it about? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 22. Lets start with Messaging MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 23. I guess that means method calls. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 24. In class based OOP, the methods of a class define its Interface. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 25. Interface == Related Methods MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 26. Quite similar to related Functions within a Namespace MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 27. So OOP is about grouping methods together? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 28. "... local retention and protection ..." ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 29. Oh yeah, objects have states. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 30. What is a state? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 31. State in class based OOP: class Foo { private static $class_state = 42; private $instance_state = -0.2; } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 32. State in RL is more like public function __construct(...) { $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames(); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 33. What properties do these examples of state have? public function __construct(...) { $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames(); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 34. A literal $this->type = 'simple'; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 35. A shared object $this->customerSession = $customerSession; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 36. Literal zero (but looks like that might change) $this->counter = 0; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 37. Global state $this->id = $_REQUEST['id']; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 38. Stuff from the DB $this->names = $db->getNames(); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 39. Can we partition those examples? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 40. 1. State that is always the same 2. State that might change (within the lifespan of the object) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 41. State that might change: → Counter → Session → Filesystem / DB / Internet → Globals MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 42. Non-changing state → The Type ID → Application State → Configuration MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 43. What can we do with this information? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 44. Pure Functions! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 45. Pure functions? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 46. → Don't cause any side effects → Given the same arguments always return the same result MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 47. What are side effects? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 48. → Changing state non-local to the function scope → DB or Filesystem or Network access → Throwing Exceptions → Forking MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 49. No side effects was one part. What was the other? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 50. Same Arguments ==> Same Result MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 51. A function that always returns the same result given the same arguments is called Referentially Transparent MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 52. Referentially Transparent Functions must not use state that might change! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 53. Why should we care? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 54. Benefits of Pure Functions! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 55. → Easier to think about (reasonability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 56. → Easier to break apart (decomposability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 57. → Easier to combine (composability & reuseability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 58. → Easier to show correctness (testability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 59. → Easier to parallelize (threadability ;)) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 60. Mkay. But what about the changing things? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 61. How can we deal with changing state? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 62. We treat objects as snapshots of the state of the world in one moment. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 63. The state of the world in that moment will never change. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 64. If change occurs, it means now is a different moment in time. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 65. We can represent change in two ways: → by modifying the object → by creating a new instance MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 66. Immutability MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 67. For example, what happens when an admin logs in? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 68. $loggedInAdmin = $loggedOutAdmin->login(); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 69. public function login() { return new self($isLoggedIn = true); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 70. But what if a method needs the "current instance"? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 71. Either that object has to be recreated with the current instance, or the current instance has to be passed as a method argument. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 72. OOP style $authorizationManager->hasAccess($dashboard, $loggedInAdmin); $authorizationManager->hasAccess($dashboard, $loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 73. In FP $has_access($dashboard, $loggedInAdmin); $has_access($dashboard, $loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 74. There are more parameters in RL! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 75. Okay, lets reduce the argument count by adding properties to the object. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 76. $dashboardAuthorizationManager = new class($authorizationManager, $dashboard) { private $authorizationManager; private $page; public function __construct($authorizationManager, $page) { $this->authorizationManager = $authorizationManager; $this->page = $page; } public function hasAccess($admin) { return $this->authorizationManager ->hasAccess($this->page, $admin); } } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 77. Only one argument left: $pageAuthorizationManager->hasAccess($loggedInAdmin); $pageAuthorizationManager->hasAccess($loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 78. How might that look like in FP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 79. $has_access_to_dashboard = function ($admin) use ($dashboard, $has_access) { return $has_access($dashboard, $admin); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 81. Can we remove all arguments? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 82. $loggedInAdminDashboardAuthorization = new class($dashboardAuthorizationManager, $loggedInAdmin) { private $pageAuthorizationManager; private $admin; public function __construct($pageAuthorizationManager, $admin) { $this->pageAuthorizationManager = $pageAuthorizationManager; $this->admin = $admin; } public function hasAccess() { return $this->pageAuthorizationManager ->hasAccess($this->admin); } } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 83. In FP: $has_logged_in_admin_access_to_dashboard = function () use ($has_access_to_dashboard, $admin) { return $has_access_to_dashboard($admin); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 84. We can bundle state and functions without classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 85. We can bundle state and process without classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 86. Look Ma, OOP in PHP without hands classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 87. We have the choice: → pass state with method arguments → set state as object properties MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 88. The more volatile the object state, the more often we need to create new instances... MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 89. ...if we want to have Pure Functions. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 90. Lesson from FP: Distinguish between mutable and immutable object properties. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 91. "Mutable stateful objects are the new spaghetti code" ~ Rich Hickey MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 92. Summary MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 93. Concepts from the FP languages can be useful in OO PHP. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 94. We can benefit from designing our OO code so it exhibits the properties of pure functions. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 95. We barely scratched the surface. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 96. Don't be afraid of FP's mathematical pattern names. It's still just code. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 97. Building these slides I learned something about OOP. I hope you found it interesting, too! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  • 98. (if (has-communication? you vinai) (communicate you vinai) (enjoy you remaining-agenda)) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp