Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBalazs Peak
46,160 PointsIn a class, when do you make a property only, and when do you make a field and a property to that field?
I'm just trippin' about a practical example.
3 Answers
David Lin
35,864 PointsIf the calculations required to construct your intended return value can be completely confined in a single place (e.g. within a property block), then it makes sense use a property to straightforwardly represent it.
But if the calculations need to be spread out over many methods all over your class, or if it requires some significant amount of time (e.g. due to a network request or database access, etc.), then it might make more sense to create a private field for it to be used solely within the class during value construction, but expose the final value externally as a public property. This gives you the added flexibility to return a default value for the property if the final value hasn't been fully constructed yet.
David Lin
35,864 PointsI think Properties were introduced as a convenience, since the pattern of creating getter/setter functions for class attributes is extremely common. Properties help to reduce the number of lines of code you need to write to achieve the same thing manually with functions, thus helping to improve code readability. I'm not clear about the lower level details of how properties are actually implemented, but it makes sense that the property name itself becomes effectively a "super-field" in a sense.
Balazs Peak
46,160 PointsThe one thing that is still not clear, how is it even possible for properties to work by themselves. I thought that properties are functions, and that functions need to modify a field, to even store that value in any way. I think properties must be something more, than just functions. They must have a "hidden field" for themselves, to store the value.... what do you think?
Balazs Peak
46,160 PointsBalazs Peak
46,160 PointsThank you David! This makes a lot of sense! I was wondering because in the universtity, they've teached us to use (private) fields, and access them through property technology. Then, I see that in treehouse courses, it's more likely to use properties only. It made me wonder, how it is in practice, since none of these are "real projects", it's just education.
You seem like you know a lot of what you are talking about! :D I'm on the verge of my developer career, I would appriciate to have a friend who is more experienced. If you are on board, you can have me on facebook.com/puklibalazs . NOTE that I have also a lot of ideas to make startups, maybe having me as a friend will be also beneficial for you one day! :D Thank you for your answer, I appriciate it very much, it made a lot of sense, and it feels like I know more about how programming actually works in practice!
The one thing that is still not clear, how is it even possible for properties to work by themselves. I thought that properties are functions, and that functions need to modify a field, to even store that value in any way. I think properties must be something more, than just functions. They must have a "hidden field" for themselves, to store the value.... what do you think?
David Lin
35,864 PointsDavid Lin
35,864 PointsYou're welcome Balaz. Glad to help a little. I don't use FB much, but there're links to my LinkedIn and Google accounts in my profile page if you'd like to connect. Dave