Sort of kidding.
I have come to prefer using switch instead of if / else (if). (If there's only one branch then if is fine.)
I just wrote the following as an extension of String:
func fixLength(_ length: Int) -> String {
if self.count < length {
return self + String(repeating: " ", count: length - self.count)
}
else if self.count > length {
return String(self.dropLast(self.count - length))
}
return self
}
While I realize its a very common idiom, I don't care for it aesthetically. I've tried two alternatives using switch, and while both (seem to) work, it still seems to me there should be a better alternative.
switch true {
case self.count == length:
return self
case self.count < length:
return self + String(repeating: " ", count: length - self.count)
case self.count > length:
return String(self.dropLast(self.count - length))
}
The above option is quite readable, and almost the same as the if/else/if. Does the fact that it has to first do the compare in the case and then apply the result with the ~= operator to true make it less efficient? Or perhaps the compiler is able to optimize that away?
Then there is this one:
switch self.count {
case length: // self.count = length
return self
case ..< length: // self.count < length
return self + String(repeating: " ", count: length - self.count)
default: // self.count > length
return String(self.dropLast(self.count - length))
}
Took me a bit to adjust to this. I guess it's OK.
Seems like a >.. postfix operator would be nice in this case, in place of the "default", i.e.:
case length >..: // self.count > length
return String(self.dropLast(self.count - length))
That doesn't seem to exist, and I've not yet attempted to create it.
What I'd truly find ideal is the following:
switch self.count {
case length: // self.count = length
return self
case < length: // self.count < length
return self + String(repeating: " ", count: length - self.count)
case > length: // self.count > length
return String(self.dropLast(self.count - length))
}
No idea if something like that is possible.
As a matter of precedence, the COBOL (yes COBOL) 2014 standard supports a similar idiom, called "partial expressions":
evaluate cnt
when length
[...]
when < length
[...]
when > length
[...]
end-evaluate
The partial expression 'selection object' of each 'when' branch is applied to the right side of the the 'selection subject' and evaluated.
Reasonable? Petty and asking for too much sugar?