Question

Working on a maintenance project, I am struggling with lot of messy code. In the current assignment, I found that the previous developer has defined property (and synthesize) for each and every iVar and IBOutlet of the class.

As per my understanding, I was used to declare properly of only those variable, which I intended to use outside of that class. And most of the time, I need not to declare property (and synthesize, of course) for any of the IBOutlet.

Can I have any Answer or Document on "When to declaring property and synthesize is needed (or essential)" ?

EDIT:

After reading the Post which Dr.Kameleon Pointed, another Question comes to my mind: What loopholes can be there in code, If I declare each and every iVar as a Propery and Synthesize them? Can I go that way?

Was it helpful?

Solution

I think in general it's hard to argue that having an extra property or two is all that bad. That being said, including lots and lots of properties and variables can clutter things up and make the code difficult to understand and maintain. And you don't really need an IBOutlet for every frazmabob in your view. So if you're looking to trim the fat a bit, just get rid of anything you don't need to manipulate at runtime. It sounds simple because it is - the only reason to include a variable/property is if you anticipate using it at runtime.

As for presenting properties to the outside world, you're right, it is a primary concept of OO programming to hide things when they don't need to be presented. This is very important if you are part of a team and you don't want others mucking with your data. So go for it - clean up that code and make life easier for yourself and others down the road.

OTHER TIPS

There is no as such rule about when to use property and when to use ivar. Using both have their own advantage-

Using ivar-

  1. using ivar provide small performance benefit.
  2. User has to do memory management manually by reference counting

Using Property-

  1. this ensures KVO compliance code.
  2. By using property, memory management is taken by Accessors.

You can checkout following topic for more insight information - http://cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html

In my opinion, i always prefer to use property as it has lot of benefit over ivars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top