Swift style guide

I know you're asking for official guidelines, but for curiosity's sake I'll describe how I do it.

In your example, I would write:

/// I put three slashes above every "unit"
/// where a "unit" is non-rigorously defined
/// as simply whatever looks to me like a unit -
/// this serves a visual aid for parsing the code at a glance
/// and once in a while when it seems appropriate I put a comment
class Car {

    ///
    var name: String
    var price: Int
    
    ///
    init
        (name: String,
         price: Int) {

        ///
        self.name = name
        self.price = price
    }

    ///
    func update
        (name: String,
         price: Int) {

        ///
        self.name = name
        self.price = price
    }

    ///
    func someLongFunctionNameWithInputsAndGenericParamsAndReturnValue
        <Generic: Parameters,
         GetTheirOwnLine: And>
        (theInputs: Array<Generic>,
         areEasilyReadable: Bool,
         becauseTheirHorizontalLocation: CGFloat,
         isNotDependentOnTheLengthOfTheFunctionName: Bool)
    async throws -> (effectsAndReturnValue: String, areOnTheirOwnLine: String)
        where Generic.WhereClause: GetsItsOwnLineToo {

        ///
        ("Similar approach for formatting", "within the function body")
    }

    ///
    func shortFunctionSignatures () -> CanBeJustOneLine {
        ...
    }
}

One result is that almost all of my code could be comfortably viewed on a vertical iPhone.

1 Like