If it must be done, I prefer the if let x
spelling. But I rarely run into this situation, in large part because I believe strongly in decorating names based on their scope. So all property references are prefixed with self.
, and parameter names (but not argument labels!) are prefixed with in
, out
, or io
, depending on their semantics. Only local variables start with a lower-case letter and get no decoration (function calls have the (
to decorate them).
I really despise seeing if let foo = foo
in other people’s code, but there are a lot of Swift conventions I don’t particularly like.
I also don't believe the initial example of fooAutomationViewController
is a very convincing one. I frequently abbreviate names, or at least make them generic, as another poster suggested. In this case, I’d probably abbreviate it vc
for viewController
, as I often do, or controller
if I feel the need to be more verbose. If there is more than one, then it definitely gets a more distinguishing name.* The long names can actually get in the way, lengthening lines and cluttering the code. For me, at least, that makes it harder to read.
And I very much am for descriptive names, even if long, in types and functions. Only local variables or parameter names that can be seen easily (i.e. functions fit on screen) get aggressive abbreviation.
Clearly a great many people do not feel the same way, but I wanted to chime in and give an alternate opinion.
*In the past I've commonly had to go through two UIViewControllers, but I like having lots of temporary locals to aid in debugging when stepping through. So, for example, when handling a segue that led to a UINavigationController
, it was common to do something like this (from memory:
func prepareSegue(identifier inID: String, destination inDestinationController: UIViewController) {
switch (inID) {
case .whatever:
let nc = inDestinationController as! UINavigationController
let vc = nc.topViewController
vc.model = myModel
…
}