samedi 21 janvier 2017

Are Encapsulating Classes Reflective of Swift 3 Paradigms?

Premises:

  • Ideally, the manner in which one programs in a language should try to match the paradigms to which the language ascribes.
  • In Swift, those paradigms are primarily protocol oriented programming, and secondarily functional programming.
  • For reasons of compatibility and interoperability, Swift also supports object oriented programming.
  • Reference types (Classes) are idiomatic of OOP, while value types (structs, enums, primitives) in conjunction with protocols, are idiomatic of POP.

Conclusion: Whenever possible, one should use reference types and protocols, and revert to Classes only when it is entirely necessary.

Inquiry: What then, is the role of a Class containing value types? Do these encapsulating classes align with Swift paradigms, or are they holdovers from OOP?

Base Case: Is the following good or bad practice in Swift:

struct Attribute {
    let name: String
    var value: Int
}

final class AttributeManager {
    var attributes: [Attribute] = []
    func add(attribute: Attribute) { self.attributes.append(attribute) }
}

Aucun commentaire:

Enregistrer un commentaire