Why are instance variables considered bad practice by Apple? Why are instance variables considered bad practice by Apple? objective-c objective-c

Why are instance variables considered bad practice by Apple?


Even though right now your private variable might work as a plain variable, you may decide later that some of properties 'goodies' are useful:

  • observing
  • atomic accessors
  • custom accessors
  • logging on access
  • access from subclasses

If you only access your variables as properties, you don't add much overhead (except in tight cycles) and leave room for reaping any of those benefits described above.

Basically, properties are useful even if you don't ever plan on making them public.

Of course, there are places where using an instance variable is still 'more natural', for example if you reimplement Foundation classes in a class cluster (such as NSArray), it's expected that your implementation is private and performant, so I don't use properties then.

Also, I don't think you should read too much into the advice. To me it reads more like "If you've only learned 5 minutes ago about properties and instance variables, let's start with using properties first".

People who are new to the language can go quite far without knowing what the instance variables are.


In other words they are strongly recommending that you use private properties rather than instance variables for any private object state.

Where did you read that they are recommending private properties? I think they mean public variables/properties.

And of course using properties instead of public instance variables has a lots of advantages:

  • encapsulation and custom getters/setters
  • memory management
  • KVO
  • binary compatibility
  • and so on

But in my opinion using private properties in general has no advantages and it's much easier to use private instance variables. The only reason I can imagine is to make custom getters/setters for such variables in future, but I don't think that it's a "best practice".


The point is underlaying storage abstraction. So simple yet very powerful.