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

iOSDevUK 2015: Clean Code through Dependency Injection

AppFoundry
September 15, 2015

iOSDevUK 2015: Clean Code through Dependency Injection

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

AppFoundry

September 15, 2015
Tweet

More Decks by AppFoundry

Other Decks in Programming

Transcript

  1. Clean Code through 

    Dependency Injection

    View full-size slide

  2. Your host
    Mike Seghers
    Developer/Architect
    [email protected]
    @mikeseghers

    View full-size slide

  3. Creating apps: a recent history

    View full-size slide

  4. 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
    }

    View full-size slide

  5. 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)

    View full-size slide

  6. Coding Example
    Acceptable in 2013
    struct FlatDesigner {
    func createDesign(requirements: Requirements) -> Design {
    //Draws some flat boxes, applies less color, returns Design
    }
    }
    struct ObjectiveCDeveloper { … }

    View full-size slide

  7. 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)

    View full-size slide

  8. 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 { … }

    View full-size slide

  9. 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)

    View full-size slide

  10. How many app factories does one
    need?

    View full-size slide

  11. 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 {}

    View full-size slide

  12. 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)
    }
    }

    View full-size slide

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

    View full-size slide

  14. Coding Example
    Acceptable all the time
    let developer = ObjectiveCDeveloper()
    let designer = FlatDesigner()
    let playingItSaveFoundry = AppFoundry(developer: developer,
    designer: designer)
    playingItSaveFoundry.createApp(requirements)

    View full-size slide

  15. Dependency Injection

    View full-size slide

  16. Helps with reusable code

    View full-size slide

  17. Dependency Injection

    View full-size slide

  18. Helps with loose coupling

    View full-size slide

  19. Dependency Injection

    View full-size slide

  20. Helps with (unit) tests

    View full-size slide

  21. Dependency Injection

    View full-size slide

  22. Helps keep your code clean

    View full-size slide

  23. Questions?
    Come talk to me!
    Mike Seghers
    Developer/Architect
    [email protected]
    @mikeseghers
    https://github.com/appfoundry/Reliant

    View full-size slide