Building for perfection

36
Building for Perfection Jorge D. Ortiz Fuentes MADRID · NOV 27-28 · 2015

Transcript of Building for perfection

Page 1: Building for perfection

Building for PerfectionJorge D. Ortiz FuentesMADRID · NOV 27-28 · 2015

Page 2: Building for perfection

Building for Perfection

Jorge D. Ortiz Fuentes @jdortiz

#Swift4AA

Page 3: Building for perfection

A Canonical Examples

production

#Swift4AA

Page 4: Building for perfection

#Swift4AA

Agenda

What is an advanced architecture?

Paradigms

Principles

Design Patterns

Conclusions

Page 5: Building for perfection

Advanced Architecture

Page 6: Building for perfection

#Swift4AA

Advanced Architecture

High level structures

• Effectiveness

• Efficiency

• Robustness

• Beauty and elegance

Page 7: Building for perfection

#Swift4AA

Architectural BenefitsReduce complexity

Maximize re-use

Reduce repetition

Basis for easier growth

Better testability

Readability and easier collaboration

Page 8: Building for perfection

– Salvador Dalí

“Have no fear of perfection, you’ll never reach it”

Page 9: Building for perfection

Not just one

Page 10: Building for perfection

But there are wrong ones

Most likely yours

😜

Page 11: Building for perfection

Wrong one

Page 12: Building for perfection

Paradigms

Page 13: Building for perfection

#Swift4AA

Programming Paradigms

Obj

ect

Orien

ted

Imperative PrgmDeclarative Prgm

Control Flow

Data Flow

Page 14: Building for perfection

#Swift4AA

Is Swift a Functional Language?

Purity or pragmatism?

Swift allows to express solutions declaratively

Many standard functions to help and even more from some libraries

Page 15: Building for perfection

Principles

Page 16: Building for perfection

#Swift4AA

Principles

Single responsibility

Open/Closed

Liskov

Interface Segregation

Dependency Inversion

Page 17: Building for perfection

Design Patterns

Page 18: Building for perfection

#Swift4AA

Syntactic Fructose

guard

defer

repeat

Page 19: Building for perfection

Guard

func presentCell(cell: SpeakerCellProtocol, indexPath: NSIndexPath) { let index = indexPath.row guard index < speakers.count else { return } let speaker = speakers[index] cell.displayName(speaker.name) cell.displayTitle(speaker.title) cell.displayDateSubmitted(relativeDateStringFromDate(speaker.dateSubmitted)) }

Page 20: Building for perfection

#Swift4AA

Swift abstractions: protocols

Swift doesn’t have abstract classes

Usage of the protocol extensions

Don’t “translate” design patterns to Swift

Page 21: Building for perfection

protocol Exportable { func export(text: String) }

extension Exportable { func export() {} }

class MarkdownDocument: Document, Exportable { }

Trait

class HTMLExporter { func export(text: String) -> String { // Generate } }

class MarkdownDocument: Document { let exporter = HTMLExporter() }

Page 22: Building for perfection

Poor Templateclass Exporter { func exportToHTML() -> String { let header = generateHeader() let body = generateContent() let footer = generateFooter() return header + body + footer }

final func generateHeader() -> String { //Implementation return "<html>" }

func generateContent() -> String { fatalError() return “" } func generateFooter() -> String { //Implementation return “</html>" } }

Page 23: Building for perfection

Swifty Templateprotocol Exporter { func exportToHTML() -> String func generateHeader() -> String func generateContent() -> String func generateFooter() -> String }

extension Exporter { final func exportToHTML() -> String { let header = generateHeader() let body = generateContent() let footer = generateFooter() return header + body + footer } final func generateHeader() -> String { //Implementation return "<html>" } func generateFooter() -> String { //Implementation return "</html>" } }

Page 24: Building for perfection

#Swift4AA

Protocol Extensions

Adds to class API vs subsystem (composition)

Reuse

DRY

Page 25: Building for perfection

#Swift4AA

Error Handling

Elegant implementation

Extended to current Cocoa for free

Errors subclass ErrorType

Page 26: Building for perfection

var control = Houston(fuel: 1.0, astronaut: nil, spaceshipOK: true)

do { try control.launchSpaceship() } catch Houston.LaunchError.NoFuel { // Add Fuel print("Adding fuel") } catch Houston.LaunchError.NoAstronaut { print("Next in line") } catch Houston.LaunchError.BrokenShip(let problem) { print(problem) } catch let unknowError { // }

Ready for Lifeclass Houston { let fuel: Double let astronaut: String let spaceshipOK: Bool init (fuel: Double, astronaut: String?, spaceshipOK: Bool) { self.fuel = fuel self.astronaut = astronaut ?? "" self.spaceshipOK = spaceshipOK } enum LaunchError: ErrorType { case NoFuel, NoAstronaut, BrokenShip(String) } func launchSpaceship() throws { guard fuel >= 1.0 else { throw LaunchError.NoFuel } guard astronaut != "" else { throw LaunchError.NoAstronaut } guard spaceshipOK else { throw LaunchError.BrokenShip("Engine") } print("Launching spaceship") } }

Page 27: Building for perfection

#Swift4AA

Generics

Aimed at DRY

Much better that id (Obj-C) or AnyObject/Any

And use where for further restrictions

Page 28: Building for perfection

Generic Awesome Power

func countGreater<T where T:Comparable>(array: [T], value: T) -> Int { var count = 0 for elem in array where elem > value { count++ } return count }

Page 29: Building for perfection

#Swift4AA

If you scratch my back…

It is also true that some patterns make using Swift easier

Prefer non-optionals: Null Object (and combine it with errors)

Page 30: Building for perfection

class Text { func displayContents() { print("Hola") } }

class NullText: Text { override func displayContents() { } }

func fetchText() -> Text { return NullText() }

let text = fetchText() text.displayContents()

Null Object

class Text { func displayContents() { print("Hola") } }

func fetchText() -> Text? { return nil }

if let text = fetchText() { text.displayContents() }

Page 31: Building for perfection

#Swift4AA

Still PendingIntrospection

• Very limited

• Useful for: Dependency Injection, KVC/KVO

Dynamic Dispatching

Community Supported features:

• Promises

• Reactive Extensions

Page 32: Building for perfection

Conclusions

Page 33: Building for perfection

#Swift4AA

Conclusions

Swift is a very expressive language

• Intent

It is mature enough to be used with advanced architectures

Some additional features would be more than welcome

Page 34: Building for perfection

Thank you!

Page 35: Building for perfection

@jdortiz #Swift4AA

Page 36: Building for perfection

MADRID · NOV 27-28 · 2015