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

Power of Abstraction

Dale Attree
September 29, 2016

Power of Abstraction

So you use inheritance, but still have huge amounts of "if" and "switch" statements cluttering up your code. You thought classes and inheritance was suppose to help keep your code clean and easy to read. Why I am still having to duplicate pieces of code that should be reusable? Then abstraction is what you are missing. Parenting is not just important in life, but in development as well.

Dale Attree

September 29, 2016
Tweet

More Decks by Dale Attree

Other Decks in Programming

Transcript

  1. Dale Attree • Senior Software Developer at Afrihost • Being

    dev’ing for 17 years • Love learning new technologies and new ways of doing things • @daleattree • Github account via daleattree.co.za
  2. Power of Abstraction • So you use inheritance, but still

    have huge amounts of “if” and “switch” statements cluttering up your code. • You thought classes and inheritance was suppose to help keep your code clean and easy to read. • Why I am still having to duplicate pieces of code that should be reusable.
  3. Then abstraction is what you are missing. Parenting is not

    just important in life, but in development as well.
  4. So abstraction is not the be all and end all

    of Object Orientated Programming. It is just another arrow in your quiver for you to use in writing solid, maintainable code. So for the nay-sayers out there, yes, there are other ways to achieve the same result. PHP is of course the double-claw hammer. I just like this approach and hopefully some of you will too.
  5. So what is abstraction? Through the process of abstraction, a

    programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. In the same way that abstraction sometimes works in art, the object that remains is a representation of the original, with unwanted detail omitted. The resulting object itself can be referred to as an abstraction, meaning a named entity made up of selected attributes and behavior specific to a particular usage of the originating entity http://whatis.techtarget.com/definition/abstraction
  6. So what is abstraction? In short, it is about getting

    to the point! Abstraction allows you to generate stripped down objects and processes, making the code you are working with more relevant to the task at hand.
  7. Yes! Abstraction extends inheritance and data encapsulation. Like I said

    earlier, abstraction is another arrow in your quiver.
  8. So what does this all mean? • You can create

    a base class that cannot be instantiated which ensures that if anyone wants to use it, they must extend it. • Like with an interface, you can force the extending class to implement certain methods • Provide default functionality of methods • All function overriding as well as parent integration
  9. But why? • By creating a base abstract class, you

    create a common foundation for workflow and/or data management • You ensure consistency throughout the hierarchy • Reusable code • Customised functionality without the need to change the base functionality
  10. At the end of the day Abstraction is a very

    handy approach to simplify complex processes and/or data structures without the need to remove capability. It helps maintain consistency and maintainable and promotes reusability. It is definitely not a silver bullet for everything, but can be for certain things.