Concerning Variable Names: Suppose there is some code similar to the following:
class AgentModel
var name: String?
var office: String?
var phone: String?
Would naming-convention dictate that these be named as
class AgentModel
var nameString: String?
var officeString: String?
var phoneString: String?
I haven't been able to find if Apple definitively lays out guidelines for whether one practice is preferred or not, but it doesn't seem Swift-y to always follow the latter, except when necessary.
Concerning Object Names: Similarly, should the object even be named
AgentModel
or should it be named as follows?
Agent
At first glance, I would think that this would follow the same conventions. Am I correct, or would this be an exception to the rule?
Do people have thoughts and/or references for this? Thanks!
If I needed to know the type and it wasn't obvious from context, I would just option click it and Xcode will give me its declaration, including its access specifier, type and mutability.
The API Design Guidelines on swift.org state (emphasis added):
Omit needless words. Every word in a name should convey salient information at the use site.
More words may be needed to clarify intent or disambiguate meaning, but those that are redundant with information the reader already possesses should be omitted. In particular, omit words that merely repeat type information.
And:
Name variables, parameters, and associated types according to their roles, rather than their type constraints.
So
var nameString: String?
var officeString: String?
var phoneString: String?
is clearly not recommended.
var name: String?
var office: String?
var phone: String?
is good but may be improved further, such as using phoneNumber
instead of phone
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With