iOSDevUK 2015: Clean Code through Dependency Injection

103e1ebcacd620770cf32a36b9aba17e?s=47 AppFoundry
September 15, 2015

iOSDevUK 2015: Clean Code through Dependency Injection

This is a 5 minute fast-talk we gave at iOSDevUK 2015.

103e1ebcacd620770cf32a36b9aba17e?s=128

AppFoundry

September 15, 2015
Tweet

Transcript

  1. Clean Code through 
 Dependency Injection

  2. Your host Mike Seghers Developer/Architect mike@appfoundry.be @mikeseghers

  3. Creating apps: a recent history

  4. 2012

  5. Coding Example Acceptable in 2012 struct SkeumorphicDesigner { func createDesign(requirements:

    Requirements) -> Design { //Draws some boxes with stitches, applies colors //returns Design } } struct ObjectiveCDeveloper { func createApp(requirements: Requirements, design: Design) -> App { //Does some code between [], applies the design using //InterfaceBuilder, returns App }
  6. Coding Example Acceptable in 2012 class AppFactory2012 { private let

    developer:ObjectiveCDeveloper private let designer:SkeumorphicDesigner init() { developer = ObjectiveCDeveloper() designer = SkeumorphicDesigner() } public func createApp(requirements:Requirements) -> App { let design = designer.createDesign(requirements) return developer.createApp(requirements, design: design) } } … let appFactory = AppFactory2012() let app = appFactory.createApp(requirements)
  7. 2013

  8. Coding Example Acceptable in 2013 struct FlatDesigner { func createDesign(requirements:

    Requirements) -> Design { //Draws some flat boxes, applies less color, returns Design } } struct ObjectiveCDeveloper { … }
  9. Coding Example Acceptable in 2013 class AppFactory2013 { private let

    developer:ObjectiveCDeveloper private let designer:FlatDesigner init() { developer = ObjectiveCDeveloper() designer = FlatDesigner() } public func createApp(requirements:Requirements) -> App { let design = designer.createDesign(requirements) return developer.createApp(requirements, design: design) } } … let appFactory = AppFactory2013() let app = appFactory.createApp(requirements)
  10. Current

  11. Coding Example Acceptable in now struct SwiftDeveloper { func createApp(requirements:

    Requirements, design: Design) -> App { //Does some code without semi-colons, applies the //design using StoryBoards, returns App } struct FlatDesigner { … }
  12. Coding Example Acceptable in now class AppFactory2014 { private let

    developer:SwiftDeveloper private let designer:FlatDesigner init() { developer = SwiftDeveloper() designer = FlatDesigner() } public func createApp(requirements:Requirements) -> App { let design = designer.createDesign(requirements) return developer.createApp(requirements, design: design) } } … let appFactory = AppFactory2014() let app = appFactory.createApp(requirements)
  13. How many app factories does one need?

  14. Coding Example Acceptable all the time protocol Designer { func

    createDesign(requirements: Requirements) -> Design } protocol Developer { func createApp(requirements: Requirements, design: Design) -> App } extension SkeumorphicDesigner : Designer {} extension FlatDesigner : Designer {} extension ObjectiveCDeveloper : Developer {} extension SwiftDeveloper : Developer {}
  15. Coding Example Acceptable all the time class AppFoundry { private

    let developer:Developer private let designer:Designer init(developer: Developer, designer: Designer) { self.developer = developer self.designer = designer } func createApp(requirements:Requirements) -> App { let design = designer.createDesign(requirements) return developer.createApp(requirements, design: design) } }
  16. Coding Example Acceptable all the time let developer = ObjectiveCDeveloper()

    let designer = SkeumorphicDesigner() let oldSchoolFoundry = AppFoundry(developer: developer, designer: designer) oldSchoolFoundry.createApp(requirements)
  17. Coding Example Acceptable all the time let developer = ObjectiveCDeveloper()

    let designer = FlatDesigner() let playingItSaveFoundry = AppFoundry(developer: developer, designer: designer) playingItSaveFoundry.createApp(requirements)
  18. Dependency Injection

  19. Helps with reusable code

  20. Dependency Injection

  21. Helps with loose coupling

  22. Dependency Injection

  23. Helps with (unit) tests

  24. Dependency Injection

  25. Helps keep your code clean

  26. Questions? Come talk to me! Mike Seghers Developer/Architect mike@appfoundry.be @mikeseghers

    https://github.com/appfoundry/Reliant