at DJ Alexander • Working in web application development since 13+ years. • Twitter: @kapilsharmainfo • Facebook: /kapilsharmainfo • Linked in: kapilsharmainfo • Website: kapilsharma.info • Slides: speakerdeck.com/kapilsharma 2
India. • Conducts regular meet ups • meetup.com/phpreboot • phpreboot.com • Twitter: @phpreboot • Facebook: /PHReboot • Slack: phpreboot.signup.team • Youtube: www.youtube.com/channel/ UCbLir8nYAFJ5YpcbjEFzjVw 3 Please subscribe, so that we can get custom Channel URL like youtube.com/phpreboot (Need 100 subscribers to get custom URL) Sponsored by JetBrains (IDE License) DJ Alexander (Space and snacks) Ansh Systems (Space and snacks)
the internal structure of software to make it: • Easier to read (understand) • Easier to extend (Future changes) • Without changing observable behaviour of the software. 8
become hard to work with Long class Long method Long parameter list Data clumps Extract Method Extract class Replace temp with Query Introduce parameter object Decompose conditionals
but can’t extract due to many local variables. class Order { ... public function price() { return new PriceCalculator($this)->compute(); } } class PriceCalculator { private $primaryBasePrice; private $secondaryBasePrice; private $tertiaryBasePrice; public __construct(Order $order) { // copy relevant information from order object. ... } public function compute() { // long computation. ... } } class Order { ... public function price() { $primaryBasePrice = 10; $secondaryBasePrice = 20; $tertiaryBasePrice = 30; // long computation. ... } }
become hard to work with Long class Long method Long parameter list Data clumps Extract Method Extract class Replace temp with Query Introduce parameter object Decompose conditionals
Refused Bequest Alternate class with different interfaces Replace Type Code with Subclass Replace Type Code with State/ Strategy Replace Conditional with Polymorphism Replace Parameter with Explicit Methods Introduce Null Object
Middle Man Show excessive coupling A method accesses the data of another object more than its own data. One class uses the internal fields and methods of another class. $a->b()->c()->d() If a class performs only one action, delegating work to another class, why does it exist at all?
Data Class Data Clumps Dead Code Divergent Change Duplicate Code Feature Envy Inappropriate Intimacy Incomplete Library Class Large Class Lazy Class Long Method Long Parameter List Message Chains Middle Man Parallel Inheritance Hierarchy Primitive Obsession Refused Bequest Shotgun Surgery Speculative Generality Switch Statements Temporary Field
to Unidirection Change Reference to Value Change Value To Reference Change Unidirectional association to Bidirectional Collapse Hierarchy Consolidate Conditional Expression Consolidate Duplicate Conditional Fragments Decompose Conditional Duplicate Observed Data Encapsulate Field Encapsulate Collection Extract Class Extract Interface Extract Method Extract Subclass Extract Superclass Extract Variable Form Template Method Hide Delegate Hide Method Inline Class Inline Method Inline Temp Introduce Assertion Introduce Foreign Method Introduce Local Extension Introduce Null Object Introduce Parameter Object Move Field Move Method Parameterize Method Pull up Constructor Body Pull Up Field Pull up Method Push Down Field Push Down Method Preserve Whole Object Remove Assignment to Parameters Remove Control Flag Remove Middle Man Remove Parameter Remove Setting Method Rename Method Replace conditional with Polymorphism Replace Constructor with Factory Method Replace Data Value with Object Replace Delegation with Interface Replace Error Code with Exception Replace Exception with Test Replace Inheritance with Delegation Replace Magic Number with Symbolic Constant Replace Method with Method Object Replace nested Condition with Guard Clauses Replace Parameter with Explicit Methods Replace Parameter with Method Call Replace Subclass with Fields Replace Temp with Query Replace Type Code with Class Replace Type Code with State/Strategy Self Encapsulate Field Separate Query from Modifier Split Temporary Variable Substitute Algorithm