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

Geração de Código - 👍 ou 👎?

Marcelo
December 03, 2016

Geração de Código - 👍 ou 👎?

O que é? Por que fazer? É bom ou ruim? Onde vive? Do que se alimenta?

Palestra apresentada no CocoaHeads Conference 2016, em 03/12/2016.

Site do evento: http://cocoaheadsconference.com.br
Entre no nosso Slack: http://iosdevbr.herokuapp.com

Marcelo

December 03, 2016
Tweet

More Decks by Marcelo

Other Decks in Programming

Transcript

  1. !

  2. ! "

  3. !

  4. import UIKit extension UIColor { class func chd_purpleColor() -> UIColor

    { return UIColor(red: 124.0 / 255.0, green: 50.0 / 255.0, blue: 220.0 / 255.0, alpha: 1.0) } }
  5. Localizable let before = NSLocalizedString("Stores.AllCategories", comment: "") let title =

    L10n.AlertTitle // -> "Title of the Alert" // "bananas.owner" = "Those %d bananas belong to %@."; let ban = L10n.Bananas.Owner(2, "John") // -> "Those 2 bananas belong to John."
  6. UIImage let before = UIImage(named: "Banana")! let image1 = UIImage(asset:

    .Banana) let image2 = Asset.Apple.image // Alternate way
  7. Template customizável! enum Asset { static var tabBarIcnAccountActive: Image {

    return #imageLiteral(resourceName: "tabBarIcnAccountActive") } } let image = Asset.tabBarIcnAccountActive
  8. Storyboards // Initial VC let initialVC = StoryboardScene.Wizard.initialViewController() // Dedicated

    type var that returns the right type of VC (CreateAccViewController here) let createVC = StoryboardScene.Wizard.createAccountViewController()
  9. apis: - path: "/pet/{petId}" operations: - method: GET summary: Find

    pet by ID type: Pet produces: - application/json - application/xml authorizations: - oauth2 parameters: - name: petId description: ID of pet that needs to be fetched required: true allowMultiple: false type: string paramType: path responseMessages: - code: 400 message: Invalid ID supplied - code: 404 message: Pet not found
  10. models: Pet: id: Pet description: "A pet is a person's

    best friend" required: - id - name properties: id: type: integer format: int64 tags: type: array items: $ref: Tag name: type: string status: type: string description: pet status in the store enum: - available - pending - sold category: $ref: Category photoUrls: type: array items: type: string
  11. open class Pet: JSONEncodable { public var id: Int64? public

    var category: Category? public var name: String? public var photoUrls: [String]? public init() {} }
  12. public struct Pet: Equatable { public let id: Int public

    let category: Category? public let name: String public let photoUrls: [URL]? public static func == (lhs: StoreInstance, rhs: StoreInstance) -> Bool { return lhs.id == rhs.id && lhs.category == rhs.category && lhs.name == rhs.name && lhs.photoUrls == rhs.photoUrls } }
  13. Swagger Code Generator → Primeira tentativa, na época de ObjC

    → Muitas(!) regexes e hacks → Extremamente frágil!
  14. Gerador próprio (a parte boa) → Focado para a parte

    do Swagger que usamos → Pode usar convenções internas para facilitar → Código gerado mais próximo do ideal (value types, modelos imutáveis, etc)
  15. Gerador próprio (a parte ruim) → Maior rolê → Complexidade

    extra → API precisa ser bem definida (nullable)
  16. Isolamento da camada de persistência class RealmDog: Object { dynamic

    var name = "" dynamic var age = 0 } struct Dog: Equatable { let name: String let age: Int static func fromRealm(_ persistedDog: RealmDog) -> Dog { return Dog(name: persistedDog.name, age: persistedDog.age) } }
  17. Repetição manual == erros → APIs String-typed → Parse de

    JSON com chave errada → Esquecer de atualizar classe quando API muda