Stories from the other side

Download Stories from the other side

Post on 08-Jan-2017

459 views

Category:

Software

7 download

Embed Size (px)

TRANSCRIPT

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