kodecocodes / objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
http://www.raywenderlich.com/62570/objective-c-style-guide
3.1k stars 628 forks source link

Direct access to properties. #42

Closed John-Lluch closed 10 years ago

John-Lluch commented 10 years ago

The guide states:

"Direct access to instance variables that 'back' properties should be avoided except in initializer methods (init, initWithCoder:, etc…), dealloc methods and within custom setters and getters. For more information on using Accessor Methods in Initializer Methods and dealloc, see here"

Since ARC there is no longer a reason to do so. With ARC you can safely access instance variables without any issues and doing it is MUCH faster. The linked Apple doc that recommends using always property accessors is outdated. Notice that according to said document the main reason to use properties is to help on manual memory management. This is the main reason the document points to:

"Sometimes it might seem tedious or pedantic, but if you use accessor methods consistently, the chances of having problems with memory management decrease considerably. If you are using retain and release on instance variables throughout your code, you are almost certainly doing the wrong thing"

This is no longer the case. Not using instance variables is just a prejudice based on earlier assumptions. We should start using them normally in the private scope of a class, that's completely fine now.

Properties should still be used for external object access but as said there is no longer any justification for using them in the private scope of a class, including accessing instance variables of the class own properties.

ghost commented 10 years ago

While I was one of the people in favor of allowing instance variables, there are some cases where using an instance variable directly instead of the property produces different results. For example, if your property is declared with the copy attribute, I do not believe (someone please correct me if I'm mistaken) that accessing the variable directly will automatically give you the copy behavior. Of course, you are free to code it yourself, but that is more prone to errors.

Note, we are in no way suggesting that it is bad practice to use private instance variables -- I use them almost exclusively in my own code, as do many others here. But for the tutorials, the committee decided to pick one way to do things and stick with it.

That said, you'll still see instance variables in some tutorials on the site, I am sure.

ColinEberhardt commented 10 years ago

doing it is MUCH faster

I just want to call out this one point you made. For the vast majority of the code that developers write, clarity should be favoured over micro-optimisation. It is very rare that the solution to a performance problem is to simply circumnavigate properties by directly accessing instance variables.

And also, I am with @elephantronic … I am pro-ivar, but this is such a minor point, that I am more than happy to use private properties and write interesting tutorials, solve bigger issues, and (hopefully) inspire developers.

John-Lluch commented 10 years ago

I am happy I am not the only one that is pro-ivar on production code. That's in my opinion more justifiable than using properties exclusively. And that's not a micro-optimisation. A class method using exclusively properties instead of their ivars can compile into code that is as much as 4 times larger, and up to 10 times slower or more. Just look at the assembly code generated or profile it. I won't call it a micro-optimisation. The only real reason for exclusive use of properties is taste, with ARC there is no longer a compelling reason for using private properties.

I expect that at some time in the future Apple will come out with Namespaces and Static (or Final) Methods. Yes, you can currently simulate namespaces by prefixing everything, and get the functionality of static methods by implementing C functions taking 'self' as the first argument. (Interestingly an argument named 'self' is allowed, so it is not a reserved word in this context). Unfortunately the required syntax does not match Obj-C method conventions.

When Apple implements the above, direct access to ivars will no longer be regarded as a non desirable thing. It may even turn into a recommended approach, as the current major issues of the language will suddenly become solved.

ColinEberhardt commented 10 years ago

As I mentioned elsewhere, this was debated to death here #5 - we put it to the vote, and went with the majority. I doubt this will be changed!

ndubbs commented 10 years ago

The team voted to use private properties instead of instance variables. This decision is going to stick. As @ColinEberhardt mentioned, it was discussed throughly!