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

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

841a0284060fa6faa652e95a467dcc69?s=128

Marcelo Fabri

December 03, 2016
Tweet

Transcript

  1. 2.

    !

  2. 3.

    ! "

  3. 4.
  4. 5.
  5. 7.
  6. 8.
  7. 9.
  8. 10.
  9. 11.
  10. 12.
  11. 13.

    !

  12. 15.
  13. 16.
  14. 18.
  15. 19.
  16. 20.

    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) } }
  17. 22.

    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."
  18. 23.

    UIImage let before = UIImage(named: "Banana")! let image1 = UIImage(asset:

    .Banana) let image2 = Asset.Apple.image // Alternate way
  19. 24.

    Template customizável! enum Asset { static var tabBarIcnAccountActive: Image {

    return #imageLiteral(resourceName: "tabBarIcnAccountActive") } } let image = Asset.tabBarIcnAccountActive
  20. 25.

    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()
  21. 27.
  22. 29.
  23. 30.

    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
  24. 31.

    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
  25. 32.

    open class Pet: JSONEncodable { public var id: Int64? public

    var category: Category? public var name: String? public var photoUrls: [String]? public init() {} }
  26. 33.

    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 } }
  27. 34.

    Swagger Code Generator → Primeira tentativa, na época de ObjC

    → Muitas(!) regexes e hacks → Extremamente frágil!
  28. 37.
  29. 38.

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

    Gerador próprio (a parte ruim) → Maior rolê → Complexidade

    extra → API precisa ser bem definida (nullable)
  31. 41.

    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) } }
  32. 42.

    Repetição manual == erros → APIs String-typed → Parse de

    JSON com chave errada → Esquecer de atualizar classe quando API muda
  33. 48.
  34. 49.