Reputation: 5629
Let assume that I have a class like this:
class Foo {
private let userId: String
init(userId givenUserId: String) {
userId = givenUserId
}
}
When I have initializers (or functions) like the above one, so ones setting some internal variable, I like them to have their external parameter name as descriptive as it can be. On the other hand, it is also good to use the same name in the internal class implementation. But then I have to use some local parameter label and the way I do it is by adding a prefix "given" before the external parameter label. Is this a good way to do this or is there any better method or convention to solve the issue?
Upvotes: 0
Views: 115
Reputation: 199
To avoid to be ambiguous, you could write your class like that:
class Foo {
private let userId: String
init(userId userId: String) {
self.userId = userId
}
}
self keyword, will automatically take the internal var.
If you want some precision on how to have a good coding style, I recommend you : swift-style-guide
Upvotes: 1
Reputation: 967
In Swift initializers, you can use the local labels identical to property names. With self.
notation, the compiler automatically figures out which one to use for simple assignments. That way, you can have expressive external parameters and avoid the burden of defining both external and internal parameter labels.
class Foo {
private let userId: String
init(userId: String) {
self.userId = userId
}
}
Upvotes: 4