Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code Quality Series- 1 Understanding OOPs concepts

Kapil Sharma
February 10, 2018

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
Tweet

More Decks by Kapil Sharma

Other Decks in Technology

Transcript

  1. 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
  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)
  3. PHPReboot.com KapilSharma.info – Martin Fowler “Any fool can write code

    that Computer can understand. Good programmers write code that Humans can understand.”
  4. 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
  5. 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)
  6. 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?
  7. 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?
  8. PHPReboot.com KapilSharma.info Interface / Abstract class • Interface - Declare

    methods • Abstract - Declare & Define methods 16 Coding for an interface Dependency Injection
  9. PHPReboot.com KapilSharma.info Coding for an interface 17 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  10. PHPReboot.com KapilSharma.info Coding for an interface 18 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  11. PHPReboot.com KapilSharma.info Coding for an interface 19 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  12. PHPReboot.com KapilSharma.info Coding for an interface 20 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  13. PHPReboot.com KapilSharma.info Coding for an interface 21 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  14. PHPReboot.com KapilSharma.info Coding for an interface 22 Slides from Laravel

    meetup: https://speakerdeck.com/kapilsharma/laravel
  15. 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.
  16. 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.
  17. 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
  18. 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
  19. 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
  20. 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)