Object Oriented Apologetics

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Object Oriented Apologetics - Presentation Transcript

    1. Object Oriented Apologetics
      Vance Lucas
      CodeWorks 2009 Dallas
      September 27, 2009
    2. What?
      2
      Not an apology
      Greek root
      - apologia (απολογία)
      - “speaking in defense”
      To defend the use of, and
      provide rational reasoning for
    3. 3
      Who is it for?
      For People Who:
      Are “on the fence” about OOP vs procedural
      Are unconvinced of the usefulness of OOP
      Want to learn WHY they should learn about OOP
    4. 4
      Purpose: To Get You Hooked on OOP
    5. 5
      NO Academic or Mundane Examples
    6. 6
      No Shapes, Cars, Fruit, or Bicycles
    7. <?phpclass myClass {     public function myClass(){     }     public function echoMe(){         echo 'me';     } } $mine=new myClass(); $mine->echoMe();
      ?>
      7
      No “Hello World” Scripts
    8. So… Why OOP?
    9. Polymorphism
      I nheritance
      E ncapsulation
      9
    10. 10
      Polymorphism
    11. Making things that are not the same look the same
      Relies on a defined interface
      Inheritance is probably the most used method
      The ability of type A to be used like type B
      Think: Interchangeable types or components
      11
      Polymorphism
    12. 12
      Real World: Different Implementations
    13. 13
      Procedural - Inline
    14. 14
      Object-oriented – Polymorphic Interface
    15. 15
      Inheritance
    16. Extend from a parent class
      “is-a” relationship
      Creates hierarchal relationship
      Get functionality for free
      Global changes are easier
      Inherits all functions and properties from parent
      Think: A is a B, with a few differences
      16
      Inheritance
    17. 17
      Controller: Zend Framework
    18. 18
      Model: phpDataMapper
    19. 19
      Warning: Keep Hierarchy Shallow
    20. 20
      Encapsulation
    21. Hide specific implementation details
      Reveal only methods and properties required to interact with the object
      Limits interdependencies between components
      Think: Separation of responsibilities
      21
      Encapsulation
    22. 22
      Payment Interface – Exposed Methods
    23. 23
      Planetoids (by Micah Jones)
      • Asteroids responsible for their own movement
      • Different: velocities, sizes, shapes, rotations, color, and fragment pieces
      • All the code and math calculations for movement are encapsulated behind ‘move()’
      • Allows different types of objects and asteroids to be treated the same in code – “polymorphically”
      24
      Planetoids: Code Abstraction
    24. Other OOP-Only Features?
      25
    25. Lazy-Loading
    26. Uses __get() “magic” method in PHP5 object model
      Also uses SPL interfaces to fire query on:
      count()
      foreach()
      Used as a hook to retrieve related rows on call
      Caches results so only 1 query is fired
      Can eliminate N+1 query problem by mapping
      27
      Lazy-Loading: OOP Only
    27. Classes are actually custom types
      Can type-hint for classes or interfaces
      PHP Standard types:
      string, int, float, boolean, array, object, null
      resource
      28
      Custom Type Creation & Type-Hinting
    28. Other Reasons TO Use OOP?
    29. Easily group related properties/data
      Avoid using globals or passing/returning arrays
      Suppress errors ala ‘undefined index’
      More?
      Convenience (a.k.a. Laziness)
      30
    30. Request object: Why not $_POST?
      Data comes from multiple sources
      POST/GET, XML, JSON, etc.
      Other functions
      isAjax(), isPost(), etc.
      Sanitizing user input
      Session object: Why not $_SESSION?
      More options for saving/storing
      Database, separate server, memcached, etc.
      31
      Request / Session Objects
    31. 32
      Request Object: Convenience, too!
    32. Need an Example?
    33. E-commerce Cart: Work Scope
      • Basic categories (1-level)
      • Simple products, no options, stock, etc.
      • Simple checkout, no user accounts
      • Authorize.net integration
      • UPS rate quotes
      • Admin backend
      • Order fulfillment
      • Invoice/packing slip printing
    34. 35
      Simple Cart
    35. Simple Checkout
      36
    36. 37
      Simple UPS Integration
    37. So far it’s OK
      It works
      We finished and worked quickly
      38
      Status Check
    38. 39
      Client Message
      I talked to my next door neighbor’s cousin’s brother’s niece yesterday, and he says all the serious online stores have regular sales. That’s something I can do too, right?
      - Bob
    39. 40
      Product Sales
    40. 41
      New Code for Product Sale
    41. We also have to add this code to the admin backend for customer invoices.
      And to the email reciepts
      Sin of code duplication
      Code smell
      42
      Thoughts
    42. 43
      Client Message
      Hey,I was at the grocery store yesterday and my daughter got 2 candy bars for $1, when they were originally $0.75 each.
      I know that if I am able to do this, I’ll get a lot of sales and it will make me rich. I need to be able to do this.
      - Bob
    43. 44
      Multi-Quantity Discounts
    44. New Code, Again
      45
    45. We also have to add this code to the admin backend and other places again.
      Sin of code duplication
      We could use procedural functions for this
      Where would we put them?
      What responsibilities do they have?
      46
      Thoughts
    46. 47
      What about the future?
    47. 48
      Employee Discounts
    48. 49
      Switching to FedEx
    49. Stock Checking
      50
    50. Clearly, as the project grows, it will become a
      maintenance nightmare if we continue on the
      current path.
      We don’t want our code to be the running joke of
      the PHP community.
      51
      We need something better
    51. 52
    52. 53
      Use the right tool for the right job
    53. 54
      OOP: Right tool for this job
    54. Create a Cart class to store items
      Encapsulate the pricing logic in an Item class
      Single place to change the code
      Item is responsible for knowing it’s price (?)
      What does this imply?
      55
      Thoughts
    55. 56
      Possible Code Changes
    56. Still storing cart in session, but now we can change it later when we need to scale
      Cart gets item price so it can check quantities
      Cart is responsible for knowing other items in cart
      Better separation of responsibility
      It’s not the job of the display logic to calculate the item’s price or apply discounts
      What about changing to FedEx?
      57
      Thoughts
    57. 58
      Re-factor it into two classes
    58. Package is responsible for knowing it’s own dimensions and weight
      Quote is responsible for fetching a live rate quote from a carrier API service
      Always think in terms of responsibility
      What code is responsible for what functions?
      Where does it go in my app?
      Is this code doing too much?
      59
      Thoughts
    59. 60
      Think about how an assembly line works
    60. OOP Myths and Misconceptions
    61. 62
      Myth #1:
      OOP is about code re-use
    62. 63
      Truth:
      Re-useable code is a by-product of good OO
    63. 64
      Problem:
      There are lots of ways to make re-useable code that are not object-oriented nor good.
      It’s a bad goal.
    64. <Code with functions and includes – re-useable>
      65
      Functions are re-useable
    65. 66
      Specific implementations are re-useable
    66. <UPS-Specific API Code>
      or
      <Payment Gateway-specific API Code>
      67
      Soft interfaces are re-useable
    67. You must set goals that will help direct you to your desired outcome
      Goals narrow attention and direct efforts to goal-relevant activities, and away from perceived undesirable and goal-irrelevant actions
      Re-use as a goal does not help you write good OO code. Re-use is a by-product of good OO.
      68
      Point: Set Good Goals
    68. 69
      Myth #2:
      Objects should always be modeled after real-world objects when possible
    69. 70
      Truth:
      Objects should be modeled and built based on what you need to complete your task
    70. 71
      Problem:
      Real-world object models are almost never useful in code
    71. 72
      Real-World Objects Change
    72. 73
      You are modeling things that don’t exist
    73. 74
    74. 75
      Myth #3:
      Everything should be objects
    75. 76
      Truth:
      Make objects for only what you need to. Most of the time this is data. Don’t over-complicate your code when it’s not necessary.
    76. 77
      Problem:
      Not everything your code does can be easily represented with an object
    77. 78
      Application Flow
      MVC diagram for
      XEROX PARC 1978-79
    78. 79
    79. Programming PHP for over 10 years
      Web: http://www.vancelucas.com
      Twitter: @vlucas
      Email: vance@vancelucas.com
      GitHub: http://github.com/vlucas
      Photo Set: http://www.flickr.com/photos/30728345@N05/galleries/72157622318592067/
      80
      Vance Lucas

    + vlucasvlucas, 1 month ago

    custom

    481 views, 3 favs, 2 embeds more stats

    In defense of object-oriented programming - How and more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 481
      • 458 on SlideShare
      • 23 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 16
    Most viewed embeds
    • 22 views on http://www.vancelucas.com
    • 1 views on http://www.bigbluehat.com

    more

    All embeds
    • 22 views on http://www.vancelucas.com
    • 1 views on http://www.bigbluehat.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories