// need to be implemented func writeCode() -> Codebase { … } // need to be implemented func learn() { … } private func drinkCoffee() { … } private func copyFromStackoverflow () -> Codebase { … } } Let’s Take A Close Look To Class
those 2 declared methods public func createAwesomeProduct() -> Product { engineer.writeCode() // coupled to behaviour not class drinkCoffee() } private func drinkCoffee() { } } Let’s Take A Close Look To Class
Definition outside PostsViewController class HINT: It allows you to use AbstractFactory pattern to mock large part of functionality scope. Mocking With Protocols
PresentableSpy! // the same for CreatableSpy override func setUp() { super.setUp() presentableSpy = PresentableSpy() sut = Navigation(presentable: presentableSpy, factory: creatableSpy) } Testing With Protocols
XCTAssertTrue(presentableSpy.wasUsed) } func testIfWeCreateControllerWithDetails() { sut.goToURL(NSURL(string: "brainly://us/fixture/42")!) XCTAssertTrue(navigationCreatableSpy.wasUsed) XCTAssertEqual("fixture", navigationCreatableSpy.capturedScreen) // fixture should be name of screen } Testing With Protocols
app screens - handle url links to app - creating and displaying UIViewControllers (modally with custom transition) - switching between ‘tab’ like screen in app Only 200 loc (but strongly coupled code) Coupled with custom classes. Not easy to extend e.g. on iPad Extracting Code From God Classes
when it is assigned to a variable or constant, or when it is passed to a function. This reduces complexity by only having one owner at any point in time Value Types Vs Reference Types
= UIAlertView(title: description, message: reason, delegate: nil, cancelButtonTitle: "OK") alert.show() } } Principles Of P.O.P. !!! “Important: UIAlertView is deprecated in iOS 8.” So probably it’s time to swap to: “UIAlertController”
= UIAlertController(title: description, message: reason, preferredStyle: .Alert) self.presentViewController(alert, animated: true, completion: nil) } } Principles Of P.O.P. !!! Type „ErrorHandler” has no member presentViewController
let fourtyTwoPagesSpecification = repository.getSpecification() // Even the variable name is long read(fourtyTwoPagesSpecification) } } Let’s Get Back To Start
TicketService) } Optional Methods In Swift class TicketService: NSObject { // for Obj-C conformance weak var delegate: TicketServiceDelegate? func stop() { delegate?.ticketServiceStopped?(self) } }