Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1090583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:23:49+00:00 2026-05-16T23:23:49+00:00

Say I have a class like this: @interface MyAwesomeClass : NSObject { @private NSString

  • 0

Say I have a class like this:

@interface MyAwesomeClass : NSObject
{
@private
    NSString *thing1;
    NSString *thing2;
}
@property (retain) NSString *thing1;
@property (retain) NSString *thing2;
@end

@implementation MyAwesomeClass
@synthesize thing1, thing1;
@end

When accessing thing1 and thing2 internally (i.e, within the implementation of MyAwesomeClass), is it better to use the property, or just reference the instance variable directly (assuming cases in which we do not do any work in a “custom” access or mutator, i.e., we just set and get the variable). Pre-Objective C 2.0, we usually just access the ivars directly, but what’s the usual coding style/best practice now? And does this recommendation change if an instance variable/property is private and not accessible outside of the class at all? Should you create a property for every ivar, even if they’re private, or only for public-facing data? What if my app doesn’t use key-value coding features (since KVC only fires for property access)?

I’m interested in looking beyond the low-level technical details. For example, given (sub-optimal) code like:

@interface MyAwesomeClass : NSObject
{
    id myObj;
}
@proprety id myObj;
@end

@implementation MyAwesomeClass
@synthesize myObj;
@end

I know that myObj = anotherObject is functionally the same as self.myObj = anotherObj.

But properties aren’t merely fancy syntax for instructing the compiler to write accessors and mutators for you, of course; they’re also a way to better encapsulate data, i.e., you can change the internal implementation of the class without rewriting classes that rely on those properties. I’m interested in answers that address the importance of this encapsulation issue when dealing with the class’s own internal code. Furthermore, properly-written properties can fire KVC notifications, but direct ivar access won’t; does this matter if my app isn’t utilizing KVC features now, just in case it might in the future?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:23:49+00:00Added an answer on May 16, 2026 at 11:23 pm

    I don’t think any way is ‘better’. You see both styles in common use, so there isn’t even a usual/best practice now. In my experience, the style used has very little impact on how well I digest some implementation file I am looking. You certainly want to be comfortable with both styles (and any in between) when looking at other people’s code.

    Using a property for every internal ivar might be going slightly overboard, in terms of maintenance. I’ve done it, and it added a non-trivial amount of work that I don’t think paid off for me. But if you have a strong desire/OCD for seeing consistent code like self.var everywhere, and you have it in the back of your mind every time you look at a class, then use it. Don’t discount the effect that a nagging feeling can have on productivity.

    Exceptions- Obviously, for custom getters (e.g. lazy creation), you don’t have much of a choice. Also, I do create and use a property for internal setters when it makes it more convenient (e.g. setting objects with ownership semantics).

    “just in case”, “might” is not be a compelling reason to do something without more data, since the time required to implement it is non-zero. A better question might be, what is the probability that all the private ivars in some class will require KVC notifications in the future, but not now? For most of my own classes, the answer is exceedingly low, so I now avoid a hard rule about creating properties for every private ivar.

    I’ve found that when dealing with internal implementations, I quickly get a good handle on how each ivar should be accessed regardless.

    If you are interested, my own approach is this:

    • Reading ivars: Direct access, unless there is a custom getter (e.g. lazy creation)
    • Writing ivars: Directly in alloc/dealloc. Elsewhere, through a private property if one exists.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have code like this: class Car def test_drive!; end end class AssemblyLine
lets say I have my owndefined datatype like this: public class InformationType { private
say, I have a partial class that contains a custom property like this public
Let's say I have a class that implements the IDisposable interface. Something like this:
This is a little contrived, but say I have a class interface like this:
Say I have a class that looks like this (get/set omited): class InfoClass{ String
Lets say I have a class A that is fairly simple like this -
So let's say you have something like this: Class Person: height = 6' 0
Let's say I have a very simple PrototypeJS class that looks like this: var
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.