Share
Embed
Code Quality Series- 1 Understanding OOPs concepts
Slides of PHP Reboot meetup on 'Understanding OOPs concepts' under 'Code Quality' meetup series.
Code discussed during meetup can be found at https://github.com/phpreboot/OOPsMeetupCode
Meet up video is also uploaded to PHPReboot youtube channel (Check slide 3).
Kapil Sharma
February 10, 2018
Transcript
35 slides
-
PHPReboot.com KapilSharma.info About me • Kapil Sharma • VP Technology
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 -
PHPReboot.com KapilSharma.info About PHP Reboot • Developers’ community in Pune,
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) -
PHPReboot.com KapilSharma.info Why we are here? 4 1. Understanding OOPs
concepts Code Quality Series -
PHPReboot.com KapilSharma.info What is quality code? • Easy to read.
5 • Easy to extend. -
PHPReboot.com KapilSharma.info – Martin Fowler “Any fool can write code
that Computer can understand. Good programmers write code that Humans can understand.” -
PHPReboot.com KapilSharma.info – Edsger W. Dijkstra “Simplicity is prerequisite for
reliability” “Simple is better” -
PHPReboot.com KapilSharma.info Easy to extend 9 O of SOLID Open
Close Principle Our code must be Open for Extension But Close for Modification -
PHPReboot.com KapilSharma.info How to achieve? • Readable: Less time to
understand what code is doing. • Simple - KISS • Single responsibility (SOLID) • Small class/methods • Consistent standards • Commonly agreed Naming convention 10 Easy to read Easy to extend • Extendability: Easy to meet new requirements without breaking existing code • DRY • Open-Close (SOLID) • Small class/method • Moduler • Continuous refactoring (consider TDD) -
PHPReboot.com KapilSharma.info Object Oriented Programming (OOPs) • Encapsulation • Abstraction
• Inheritance • Polymorphism 14 Class and objects • Class • Class • Interface/Abstract class/ parent/child class • Overloading/Overriding How the hell they are going to help in code quality? -
PHPReboot.com KapilSharma.info Object Oriented Programming (OOPs) • Encapsulation • Abstraction
• Inheritance • Polymorphism 15 Class and objects • Class • Class • Interface/Abstract class/ parent/child class • Overloading/Overriding How the hell they are going to help in code quality? -
PHPReboot.com KapilSharma.info Interface / Abstract class • Interface - Declare
methods • Abstract - Declare & Define methods 16 Coding for an interface Dependency Injection -
PHPReboot.com KapilSharma.info Coding for an interface 17 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Coding for an interface 18 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Coding for an interface 19 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Coding for an interface 20 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Coding for an interface 21 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Coding for an interface 22 Slides from Laravel
meetup: https://speakerdeck.com/kapilsharma/laravel -
PHPReboot.com KapilSharma.info Code example - Problem statement • We want
to build a calculator that receives two numbers and does an operation. 23 • We do not know which kind of operations we want to do yet, we will get them in the future (future business requirements) and this calculator may have really complex operations. • Currently planned operations are: • Sum • VAT (amount, VAT rate) • Different team will be adding parallel operations to this calculator so it must be easily extendable and using dependency injection. -
PHPReboot.com KapilSharma.info Solution • Since we do not know future
operations: 25 • We must define solution using Open-Close principle • Open for extension (Can add new operation) • Close for modification (We must not edit existing operation, until there is change request. -
PHPReboot.com KapilSharma.info Defining operation • Why separate class for each
operation • Help adding future requirement, without impacting current operations. • Since one operation have one class, it follows ‘Single Responsibility Model’ and KISS (Keep It Simple and Stupid) principle. 27 -
PHPReboot.com KapilSharma.info Getting operation instance • We have different class
for different operation • Creating instance for proper operation, is separate business logic, and must reside in different class. • This is called Factory Pattern - OperationFactory 29 -
PHPReboot.com KapilSharma.info Operation Command • Now main business logic, will
remain unchanged. It simple get instance from factory and run execute method. • In example git repository, we used Symfony/console component to make executable console application. • Symfony/Console is easy symfony component, check its documentation for details. 31 -
PHPReboot.com KapilSharma.info How to achieve? • Readable: Less time to
understand what code is doing. • Simple - KISS • Single responsibility (SOLID) • Small class/methods • Consistent standards • Commonly agreed Naming convention 34 Easy to read Easy to extend • Extendability: Easy to meet new requirements without breaking existing code • DRY • Open-Close (SOLID) • Small class/method • Moduler • Continuous refactoring (consider TDD)