UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Question: Is it just a matter of CONVENTION to use the same name of property's as the names of init () parameter's ?

Forums > 100 Days of SwiftUI

@boat  

This has been confusing me for ages.

From what I see, it only creates confusions for newbies like me.

If we don't have this convention, we don't even need to use self.propertyname , right ? self. here is for differentiating the property and the parameter, right ? If these two names are different, problem solved, no ?

Or there's some good reasons behind this appraoch ?

Thank you in advance

Boat

3      

I think you're right; there's nothing stopping you from using different property names than function/method names. Sometimes it can be difficult to think of good names though - especially when the variables mean the same thing. This is particularly true with initializers, where the parameter's only purpose is to set the property value.

3      

Yep, just convention, though a useful one IMO because it makes it 100% clear how the parameters are being used in the data structure. And, as @ty-n-42 mentions, it can be difficult to come up with good names that aren't already the name of the properties those parameters will be assigned to. And why make life more difficult for yourself?

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.