WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

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



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.


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?


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.