- Mastering macOS Programming
- Stuart Grimshaw
- 156字
- 2021-07-02 22:54:32
Function naming in Swift 3.0
We often need to have more than a verb in a function symbol in order for it to be unambiguous. A function named ring, for example, could apply to a number of situations, and it is not clear from the name which is the case here. So, in the case that our ring function should, in fact, apply to a telephone number, we'll need to add something to the function name to reflect that.
The Swift 3 API guidelines have something to say on this matter, and we should adhere to those guidelines for the sake of consistency, and therefore clarity:
Don't do this:
func ringNumber(_: Int) {...}
Do this instead:
func ring(number: Int) {...}
You may or may not agree that this is more readable, and more appropriate for function overloading, but either way, conforming to the conventions of a language are an important part of learning to use it.