Apple has this old set of guidelines for naming methods in Cocoa APIs. In the Accessor Methods section of this guideline, it is stated:
If the property is expressed as a noun, the format is:
- (
type)
noun;
- (void)set
Noun :(
type)
aNoun ;
Specifically it is stated regarding the use of "get" in method names:
Use “get” only for methods that return objects and values indirectly. You should use this form for methods only when multiple items need to be returned.
- (void)getLineDash:(float *)pattern count:(int *)count phase:(float *)phase;
In methods such as these, the implementation should acceptNULL
for these in–out parameters as an indication that the caller is not interested in one or more of the returned values.
Apple seems to follow these guidelines in all its current Swift APIs, yet the official Swift.org API design guidelines are conspicuously silent regarding when it might be appropriate to use "get" in a function name.
I believe this omission was likely due to the fact property accessors have special syntax in Swift:
var foo: Int {
get { ... }
set { ... }
}
However, out in the wild I keep seeing functions that violate the old Cocoa standards, like:
func getFoo(for bar: Bar) { ... }
func setFoo(for bar: Bar) { ... }
Of course this leads to inconsistency between Swift APIs written by those who still follow the Cocoa guidelines (including Apple) and newer Swift devs who may be coming over from languages where it's normal to have "get" in function names.
I think this current situation is a direct consequence of the fact that our Swift API guidelines do not explicitly say not to use "get" for functions that return a value.
So my pitch is that we should update the Swift.org API design guidelines with the same basic guidelines about the use of "get" in method names as was found in the Cocoa guidelines:
If a property is accessed or set through a function, the format is:
func
noun() ->
Type
func set
Noun(
label:
Type)
Use “get” only for methods that return objects and values indirectly (as with
inout
or pointer parameters). You should use this form for methods only when multiple items need to be returned, e.g.:
func getLineDash(_ pattern: UnsafeMutablePointer<CGFloat>?, count: UnsafeMutablePointer<Int>?, phase: UnsafeMutablePointer<CGFloat>?)
Thoughts?