Stories from the other side

Download Stories from the other side

Post on 08-Jan-2017

453 views

Category:

Software

1 download

TRANSCRIPT

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppStories fromthe Other SideMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppExploring OOP andFunctional Programming(to become a better programmer)MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhy am I interested in this?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppSmart guys go FP (lambdalicious, phunkie)The Little SchemerScala is the rage!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhat is this, OOP + FP?Where did these paradigms start?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppThe term "Object Oriented Programming" was coined by Alan KayMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppOriginal image by Marcin Wichary - Thanks!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopphttps://flic.kr/p/5zZexJAmongst many other achievements,Alan Kay created SmalltalkMageTitans 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 KayMageTitans 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://@VinaiKoppOOP 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 downMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppHow does OOP work with that?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhat is OOP?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIs 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 KayMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppKay doesn't mention inheritance.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIs OOP about classes?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppKay doesn't mention classes either.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIf OOP is not aboutclasses or inheritance,what is it about?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppLets start withMessagingMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppI guess that means method calls.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIn class based OOP,the methods of a class define itsInterface.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppInterface==Related MethodsMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppQuite similar torelated Functionswithin a NamespaceMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppSo OOP is about grouping methods together?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp"... local retention and protection ..."~ Alan KayMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppOh yeah, objects have states.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhat is a state?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppState in class based OOP:class Foo{ private static $class_state = 42; private $instance_state = -0.2;}MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppState in RL is more likepublic 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://@VinaiKoppWhat 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://@VinaiKoppA literal $this->type = 'simple';MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppA shared object $this->customerSession = $customerSession;MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppLiteral zero(but looks like that might change) $this->counter = 0;MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppGlobal state $this->id = $_REQUEST['id'];MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppStuff from the DB $this->names = $db->getNames();MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppCan we partition those examples?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp1. State that is always the same2. State that might change(within the lifespan of the object)MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppState that might change: Counter Session Filesystem / DB / Internet GlobalsMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppNon-changing state The Type ID Application State ConfigurationMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhat can we do with this information?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppPure Functions!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppPure functions?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp Don't cause any side effects Given the same argumentsalways return the same resultMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhat areside 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 ForkingMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppNo side effectswas one part.What was the other?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppSame Arguments==>Same ResultMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppA function that always returns the same result given the same arguments is calledReferentially TransparentMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppReferentially Transparent Functionsmust not use state that might change!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWhy should we care?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppBenefitsof 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://@VinaiKoppMkay.But what about the changing things?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppHow can we deal with changing state?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe treat objects as snapshots of thestate of the world in one moment.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppThe state of the world in thatmoment will never change.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIf change occurs, it means now is a different moment in time.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe can represent change in two ways: by modifying the object by creating a new instanceMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppImmutabilityMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppFor 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://@VinaiKopppublic function login(){ return new self($isLoggedIn = true);}MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppBut what if a method needs the"current instance"?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppEither that object has to be recreatedwith the current instance,or the current instance has to bepassed as a method argument.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppOOP style$authorizationManager->hasAccess($dashboard, $loggedInAdmin);$authorizationManager->hasAccess($dashboard, $loggedOutAdmin);MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppIn FP$has_access($dashboard, $loggedInAdmin);$has_access($dashboard, $loggedOutAdmin);MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppThere are more parameters in RL!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppOkay, 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://@VinaiKoppOnly one argument left:$pageAuthorizationManager->hasAccess($loggedInAdmin);$pageAuthorizationManager->hasAccess($loggedOutAdmin);MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppHow 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://@VinaiKoppCan 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://@VinaiKoppIn 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://@VinaiKoppWe can bundle stateand functionswithout classes!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe can bundle stateand processwithout classes!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppLook Ma,OOP in PHP without hands classes!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe have the choice: pass state with method arguments set state as object propertiesMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppThe more volatile the object state,the more often we need tocreate new instances...MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp...if we want to havePure Functions.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppLesson 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 HickeyMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppSummaryMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppConcepts from the FP languages can be useful in OO PHP.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe can benefit from designing ourOO code so it exhibits the properties of pure functions.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppWe barely scratched the surface.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppDon't be afraid of FP's mathematical pattern names. It's still just code.MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKoppBuilding these slides Ilearned 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