ABC
Advice
When no strong semantic or conventional ordering exists, prefer lexicographical order. This provides a consistent, discoverable rule that reduces ambiguity for contributors.
Scope
This principle applies both to argument lists and to struct/class property declarations when no stronger convention exists.
Context
In cases where functions have an expectation around order this should be respected:
-
Naming Convention
Sometimes methods might have a strong convention around argument ordering for example when we are talking about sizes we often go with the orderwidthfollowed byheight, think Android’sSizeor Apple’sCGSize. -
Calling Convention
It’s often advised to put varargs and arguments with default values towards the end of a declaration.
In other cases it makes sense to go lexicographical to optimise for human readers. This has a couple of benefits:
-
Easy search
It’s much faster for humans to scan lists if they are in a sensible order. -
Easy insertion
By choosing lexicographical order we take away choice, which means it’s much easier for people to conform rather than trying to figure out if there is some undocumented grouping/ordering that they need to follow.
Examples
Good
struct Car {
let color: String
let make: String
let mileage: Double
let model: String
let numberOfDoors: Int
let year: Int
}
Bad
struct Car {
let make: String
let model: String
let year: Int
let color: String
let numberOfDoors: Int
let mileage: Double
}