Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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 <code>self.var</code> 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.</p> <p>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).</p> <p>"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.</p> <p>I've found that when dealing with internal implementations, I quickly get a good handle on how each ivar should be accessed regardless.</p> <p>If you are interested, my own approach is this:</p> <ul> <li>Reading ivars: Direct access, unless there is a custom getter (e.g. lazy creation)</li> <li>Writing ivars: Directly in <code>alloc/dealloc</code>. Elsewhere, through a private property if one exists. </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload