Whether the variable uses Self.foo or _foo

Source: Internet
Author: User

<title>Selfor_</title>

original link the debate over Direct access variable usage controversy Self.foo _foo

Began the discussion of this issue, haha, direct migration to Swift?? This problem also has some advantages and disadvantages in EF OC 2.0.

Original link

    • Http://inessential.com/2015/10/28/the_debate_over_direct_access
The debate over Direct access variable usage controversy

At lunch today a few of us discussed the issue of using Self.foo vs. _foo in Objective-c.

We had people on the sides. I ' ll lay out the arguments.

(Note:this isn ' t even a debate in Swift, which are one of the reasons I ' m eager to switch to Swift.)

Self.foo

This side argues against declaring Ivars, and argues the all properties-external and internal-should be synthesized ( usually implicitly).

The only cases where to access the underlying storage directly-reference _foo, in other words-is in Init, Dealloc, an D in custom accessors. Otherwise you use Self.foo everywhere.

In addition to the Init,dealloc, the custom access method, the other places are used in the form of self.

Reasons:

    • It ' s a simple rule to remember. You don ' t has to remember whether a property is internal or external. (Easy to remember)
    • A Given property can has custom accessors, or not, and you still treat it the exact same the. Adding or removing custom accessors does not mean you had to go looking for all the references to that property and Possi Bly change them. (Easy to modify)
    • You ' re treating your object ' s interface as an API. Inside The object has more APIs than outside callers can see, and which is fine. It ' s still all API. (accessed as an API)
    • You had to worry less about KVO if you always use Self.foo instead of _foo. (Don't worry about Kvo)
    • You ' re more likely to get memory management right if you use Self.foo. (This was much less of a issue with ARC, I Grant.) (Manual memory management is more convenient.)
    • Subclasses is more likely to get things right if your use Self.foo. (sub-class convenient)
    • Apple seems to advocate this style. (not always consistently.) (Apple recommends ....)
_foo

This side argues that using direct storage access inside a object is the the-go.

Reasons:

    • There ' s a useful distinction between public and private. The code for a object is private, and it's therefore fair to access storage directly. An object have one api-its public api-and inside the object can do whatever makes sense. (Clear and public)
    • Any time you send a message, it's possible that anything could happen, including reentrancy bugs and so on. Accessing storage directly does not has that problem. (easy to introduce bugs)
    • Sending a message may has performance issues. (Probably not.) But it ' s possible.) (There may be performance issues)

Okay-i Don ' t fully understand this side of the argument. I did the best I can-explain it, but I'm most definitely in the Self.foo camp.

But the smart people disagree with me.

Why am I wrong, and what is _foo better?

Undefined

Whether the variable uses Self.foo or _foo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.