Style

After reading the guides from Ray Wenderlich, and NetGuru, I recommend following closely those of a Swift GitHub collaboration. In summary:

  • use Tabs for indentation;
  • end files with a newline;
  • use whitespace to divide bits of logic;
  • use camel case for naming, starting with a lowercase letter, except…
  • names of classes, structs, enums, enum cases, typealiases, protocols and generic types should be capitalized;
  • use let instead of var wherever possible;
  • avoid force-unwrapping of Optionals, and the use of Implicitly Unwrapped Variables, i.e. declare as aType? rather than aType! And use if let = aVar? instead, or Optional Chaining;
  • avoid get on read-only properties and subscripts e.g.
var aProperty: Int {
    return 10
}

and not

var aProperty: Int {
    get {
        return 10
    }
}
  • always specify Access Control for top-level definitions (e.g. private func aFunction(aParameter: Int) -> Bool) {};
  • This is where I differ from the GitHub Collaboration: when declaring a variable, use the short form as recommended by Apple (e.g. let aFloat = 2.0, and not let aFloat: Float = 2.0);
  • only explicitly use self when absolutely required;
  • use structs rather than classes when possible;
  • make classes final unless certain that the class will be inherited.
  • names of images for Interface Builder should end in @2x for Retina devices, @3x for Retina HD devices, -568h@2x for Retina 4 devices

Leave a Reply

Your email address will not be published. Required fields are marked *