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

Framework Oriented Programming

Framework Oriented Programming

Have you ever tried to reuse code from your mobile apps and you haven’t been able to? Did you start using frameworks only when Apple suggested it for Watch Extensions? With more platforms coming out, there's a clear need of bundling logic that can be reused and shared in multiple platforms. Frameworks will help us with that and will benefit our application code bases in many ways. Learn how to do it, with or without dependency manager involved, recommendations and some caveats you must keep in mind. Start building your own Foundation frameworks, reusable, well designed, and with single responsibilities.

Pedro Piñera Buendía

May 24, 2016
Tweet

More Decks by Pedro Piñera Buendía

Other Decks in Technology

Transcript

  1. Framework Oriented Programming • Started with GitDo • Applying principles

    to SoundClound • Ideas/Principles together in a repository (Reference) • Feedback is welcome !
  2. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  3. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  4. Context 2008 Apple launches iPhone Software Development Kit ! (Developers

    move to iOS. New platform, frameworks,... New exciting area)
  5. Context After 2008 iOS == 1 Bundle* OSX == 1

    Bundle* *Xcode project target
  6. Context After 2011 iOS == 1 Bundle* OSX == 1

    Bundle* X Bundles (External Dependencies) *Xcode project target
  7. Context 2015 iOS == 1 Bundle* OSX == 1 Bundle*

    tvOS == 1 Bundle* watchOS == 1 Bundle*
  8. Swift ❤ Dynamic Frameworks (OSX, iOS, watchOS, tvOS) • Allow

    embedded resources (images, fonts, ...) • Dynamically linked (No duplicated symbols) • Swift code
  9. Framework Oriented Programming Coding your apps organizing your code base

    in reusable & multiplatform code bundles Best Practices, Principles, Advices.. github.com/pepibumur/framework-oriented-programming
  10. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  11. 6. Final SOLID inspired (open/closed) final class Person { let

    name: String } class Alien: Person { // Compiler complains }
  12. 7. Framework models Don't share lower frameworks models upwards //

    Persistence class Author: NSManagedObjectModel { let name: String } class Track: NSManagedObjectModel { let author: Author } // ListenersKit struct StreamTrackEntity { let name: String let authorName: String }
  13. 7. Framework models Don't share lower frameworks models upwards struct

    StreamTrackEntityAdapter { func adapt(track: Track) -> StreamTrackEntity { return StreamTrackEntity(name: track.name, authorName: track.author.name) } }
  14. But... There's platform specific logic Examples - No NSFetchedResultsController in

    macOS - NSIndexPath is slightly different for watchOS.
  15. 10. Core/Testing Accessible from all the Frameworks • Extensions •

    Logging • Analytics • Architectural components (e.g. Reactive)
  16. Index • Context • Principles • Advantages ! • How

    to? • Open Questions • SoundCloud attempt • Downsides • Conclusions
  17. Open Source And benefit from the community Build pieces of

    code that you'd be proud of open sourcing
  18. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  19. How to? CocoaPods • ✅ Easy setup (each Framework .podspec)

    • ✅ You don't have to worry about Xcode Frameworks configuration • ✅ Same setup for local/external dependencies • ❌ .podspec cannot point to another local .podspecs • ❌ CocoaPods sucks if you don't version.
  20. How to? Local podspec discovery # Networking ~> Core dependency

    not found pod 'Networking' pod 'Core' pod 'AppKit'
  21. How to? Local podspec discovery pod 'Core' pod 'Networking' #

    Core has already been resolved pod 'AppKit'
  22. How to? Manual • ✅ More control over the workspace

    • ❌ Cumbersome setup (Build Settings) • External dependencies can be checked out with Carthage/Git Submodules.
  23. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  24. Open Questions External Dependencies? RECOMMENDATION ⾠ • If CocoaPods for

    local: Use it also for external. • If manual setup: Use Carthage for checking out external dependencies carthage update • With the binary. • Adding the project to the workspace: --no-build
  25. Open Questions Versioning? Git repo per framework? RECOMMENDATION ⾠ 1.

    Keep it in the same repository (fast iterations) 2. Move it once it consolidates. (sporadic changes) 3. Then version it! (snapshots in time)
  26. Open Questions Static or Dynamic? RECOMMENDATION ⾠ - Objective-C &

    not shared ~> Static - Objective-C && shared ~> Dynamic - Swift && .* ~> Dynamic The more dynamic the worse load time
  27. Open Questions Migrate existing project RECOMMENDATION ⾠ - Start with

    Core/Testing - Move Foundation components down. - Continue building layers progressively. You'll figure out how couple your code is !
  28. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  29. 1. We didn't version with CocoaPods I'm not against CocoaPods!

    Local Pods + No versioning + Team = It Sucks
  30. 2. Too many frameworks • No clear responsibilities • Crossed

    dependencies • Messy dependency tree • Very small responsibilities
  31. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  32. Storyboards/Xibs in Frameworks Sucks ! Tip 1: Keep them in

    the application target Tip 2: Reuse UI only if it's in code
  33. Index • Context • Principles • Advantages • How to?

    • Open Questions • SoundCloud attempt • Downsides • Conclusions
  34. Use your commonsense When deciding the Frameworks you need (don't

    get inspired from Javascript) • Try to have only those that you really need.
  35. Configuration depends on your project • New project? • Existing

    project to migrate? • Many external dependencies? • Not that many? • Already using CocoaPods? • How many people in your team?
  36. References • Library Oriented Programming: Justin Spahr-Summers • The Unofficial

    Guide to xcconfig files • CocoaPods • Carthage • pepibumur/framework-oriented-programming • Static & Dynamic libraries • Creating your first iOS Framework