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

Code Reuse

Apokrupto
September 21, 2015

Code Reuse

Using separation by behaviour to reduce view controller responsibilities, code size and repetition.

Unfortunately the presentation has embedded video as well as three breaks for demos in Xcode, so the full effect isn't transmitted very well.

Apokrupto

September 21, 2015
Tweet

More Decks by Apokrupto

Other Decks in Programming

Transcript

  1. Cyclomatic Complexity • Number of independent paths through a •

    function • class • module © Apokrupto 2015
  2. Cyclomatic Complexity • Good < 10-12 • Review < 20

    • Refactor >20 • Unmaintainable ~30-40 • Fatally unmaintainable > 50 © Apokrupto 2015
  3. Code reuse Using separation by behaviour to reduce view controller

    responsibilities, code size and repetition © Apokrupto 2015
  4. Principles • Single responsibility • Separation of concerns • Promotes

    reuse • What do ‘rate my app’ & ‘call our helpdesk’ features have to do with each other? • And what do they have to do with the model? © Apokrupto 2015
  5. Principles • Single responsibility • Separation of concerns • Promotes

    reuse • What do ‘rate my app’ & ‘call our helpdesk’ features have to do with each other? • And what do they have to do with the model? © Apokrupto 2015
  6. Principles • Single responsibility • Separation of concerns • Promotes

    reuse • What do ‘rate my app’ & ‘call our helpdesk’ features have to do with each other? • And what do they have to do with the model? © Apokrupto 2015
  7. Principles • Single responsibility • Separation of concerns • Promotes

    reuse • What do ‘rate my app’ & ‘call our helpdesk’ features have to do with each other? • And what do they have to do with the model? © Apokrupto 2015
  8. Principles • Single responsibility • Separation of concerns • Promotes

    reuse • What do ‘rate my app’ & ‘call our helpdesk’ features have to do with each other? • And what do they have to do with the model? © Apokrupto 2015
  9. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  10. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  11. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  12. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  13. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  14. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015
  15. MVC • The ‘Massive View Controller’ problem • Unrelated functionality

    implemented in the same class • What if we have a “Call the helpdesk” button on multiple screens? • DRY • Base class? © Apokrupto 2015 Behaviours
  16. What do we get? • Animating the button’s arrival •

    Animating the background • Dismissing the button • Rating the app • Calling the helpdesk © Apokrupto 2015
  17. What do we get? • MySlidingInAndOutBackgroundAnimatingAppRat eAndHelpDeskViewController.h • 700 lines

    of code • Change requests • if-else/switch hell © Apokrupto 2015
  18. What do we get? • MySlidingInAndOutBackgroundAnimatingAppRat eAndHelpDeskViewController.h • 700 lines

    of code • Change requests • if-else/switch hell © Apokrupto 2015
  19. What do we get? • MySlidingInAndOutBackgroundAnimatingAppRat eAndHelpDeskViewController.h • 700 lines

    of (unrelated) code • Change requests • if-else/switch hell © Apokrupto 2015
  20. What do we get? • MySlidingInAndOutBackgroundAnimatingAppRat eAndHelpDeskViewController.h • 700 lines

    of (unrelated) code • Change requests • if-else/switch hell © Apokrupto 2015
  21. What do we get? • MySlidingInAndOutBackgroundAnimatingAppRat eAndHelpDeskViewController.h • 700 lines

    of (unrelated) code • Change requests • if-else/switch hell © Apokrupto 2015
  22. What do we get? • Animating the button’s arrival •

    Animating the background • Dismissing the button • Rating the app • Calling the helpdesk © Apokrupto 2015
  23. Separating the behaviours • Animating the button’s arrival • Animating

    the background • Dismissing the button • Rating the app • Calling the helpdesk © Apokrupto 2015
  24. Separating the behaviours • Take them all out of the

    View Controller • Use composition to put them back • Make them configurable • Without explicitly adding them to the VC © Apokrupto 2015
  25. Separating the behaviours • Take them all out of the

    View Controller • Use composition to put them back • Make them configurable • Without explicitly adding them to the VC © Apokrupto 2015
  26. Separating the behaviours • Take them all out of the

    View Controller • Use composition to put them back • Make them configurable • Without explicitly adding them to the VC © Apokrupto 2015
  27. Separating the behaviours • Take them all out of the

    View Controller • Use composition to put them back • Make them configurable • Without explicitly adding them to the VC © Apokrupto 2015
  28. Behaviour Ownership • The behaviour may be destroyed at any

    moment, because there are no strong references to it • We fix this by making it an associated object of the view controller • giving us the necessary ownership • and making the composition © Apokrupto 2015
  29. Facts & figures • Recent project • 115 separate behaviours

    • App level, library specific, generic • App level - highest coupling • Generic - lowest & most reusable © Apokrupto 2015
  30. Advanced • Your behaviours can be the delegates and data

    sources for your view controllers • Implement scroll, table & other view’s delegates and data sources in behaviours © Apokrupto 2015
  31. Advanced • Add delegates & data sources to your behaviours

    • localisation • can be another behaviour • or use the view controller © Apokrupto 2015
  32. Advanced • Add delegates & data sources to your behaviours

    • localisation • can be another behaviour • or use the view controller © Apokrupto 2015
  33. Advanced • Add delegates & data sources to your behaviours

    • localisation • can be another behaviour • or use the view controller © Apokrupto 2015
  34. Advanced • Add delegates & data sources to your behaviours

    • localisation • can be another behaviour • or use the view controller © Apokrupto 2015
  35. To sum up • Separating individual responsibilities results in smaller,

    more maintainable and reusable code • Greater flexibility with unit testing • Refactoring is a simple delete • Skinny is good © Apokrupto 2015
  36. To sum up • Separating individual responsibilities results in smaller,

    more maintainable and reusable code • Greater flexibility with unit testing • Refactoring is a simple delete • Skinny is good © Apokrupto 2015
  37. To sum up • Separating individual responsibilities results in smaller,

    more maintainable and reusable code • Greater flexibility with unit testing • Refactoring is a simple delete • Skinny is good © Apokrupto 2015
  38. To sum up • Separating individual responsibilities results in smaller,

    more maintainable and reusable code • Greater flexibility with unit testing • Refactoring is a simple delete • Skinny is good © Apokrupto 2015
  39. Core Data as a behaviour • Generally NSFetchedResultsController is used

    with a table view to populate a view controller • Much of this is generic • A Core Data behaviour can act as the UITableViewDelegate and UITableViewDataSource • But the behaviour does not know how to populate cells, or what to do if they are selected © Apokrupto 2015
  40. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  41. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  42. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  43. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  44. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  45. Core Data as a behaviour • Generic Core Data behaviour

    with table view outlet • Behaviour is UITableViewDataSource and UITableViewDelegate • Behaviour will proxy the results from a CD fetch request to the table view • Behaviour has a Core Data Data Source and Core Data Delegate • Data Source handles the specifics of populating a table view cell • Delegate handles the specifics of selecting a cell © Apokrupto 2015
  46. Big finish stats • Adding a golf course • Core

    Data table population • Customising the table • Background animation • Sliding animation 813 © Apokrupto 2015
  47. Big finish stats • Adding a golf course • Core

    Data table population • Customising the table • Background animation • Sliding animation 767 LOC © Apokrupto 2015
  48. Big finish stats • Adding a golf course • Core

    Data table population • Customising the table • Background animation • Sliding animation 767 LOC © Apokrupto 2015
  49. Big finish stats • Adding a golf course • Core

    Data table population • Customising the table • Background animation • Sliding animation 767 LOC 94 © Apokrupto 2015
  50. Big finish stats • Very low complexity • Highest complexity

    method: 6 • Average complexity: 1.6 • Average NLOC: 6 © Apokrupto 2015
  51. It doesn’t end there • It doesn’t have to be

    a table view • Collection views • Page views • Hand crafted views representing any collection © Apokrupto 2015
  52. It doesn’t end there • It doesn’t have to be

    a table view • Collection views • Page views • Hand crafted views representing any collection © Apokrupto 2015
  53. It doesn’t end there • It doesn’t have to be

    a table view • Collection views • Page views • Hand crafted views representing any collection © Apokrupto 2015
  54. It doesn’t end there • It doesn’t have to be

    a table view • Collection views • Page views • Hand crafted views representing any collection © Apokrupto 2015
  55. It doesn’t end there • It doesn’t have to be

    a table view • Collection views • Page views • Hand crafted views representing any collection © Apokrupto 2015
  56. Lessons Learned • Write generic code • Write low complexity

    code • Write code that is designed to be extended • Keep that code available somewhere • git, cocoapod, library, github, carthage © Apokrupto 2015