selfOR_

原文链接The Debate Over Direct Access 变量使用的争议self.foo_foo

又开始了这个问题的讨论,哈哈,直接迁移到swift??这个问题在ef oc 2.0中也有过列举各自的一些优缺点.

原文链接

  • http://inessential.com/2015/10/28/the_debate_over_direct_access

The Debate Over Direct Access 变量使用的争议

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

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

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

self.foo

This side argues against declaring ivars, and argues that all properties — external and internal — should be synthesized (usually implicitly).

The only cases where you access the underlying storage directly — reference _foo, in other words — is in init, dealloc, and in custom accessors. Otherwise you use self.foo everywhere.

除了在init,dealloc ,自定义的访问方法中,其他的地方都使用self.的形式.

Reasons:

  • It’s a simple rule to remember. You don’t have to remember whether a property is internal or external.(便于记忆)
  • A given property can have custom accessors, or not, and you still treat it the exact same way. Adding or removing custom accessors does not mean you have to go looking for all the references to that property and possibly change them.(修改方便)
  • You’re treating your object’s interface as an API. Inside the object you have more API than outside callers can see, which is fine. It’s still all API.(作为API访问)
  • You have to worry less about KVO if you always use self.foo instead of _foo.(不用担心KVO)
  • You’re more likely to get memory management right if you use self.foo. (This is much less of an issue with ARC, I grant.)(手动内存管理的时候,更加方便.)
  • Subclasses are more likely to get things right if you use self.foo.(子类化方便)
  • Apple seems to advocate this style. (Not always consistently.)(Apple推荐....)

_foo

This side argues that using direct storage access inside an object is the way to go.

Reasons:

  • There’s a useful distinction between public and private. The code for an object is private, and it’s therefore fair to access storage directly. An object has one API — its public API — and inside the object you can do whatever makes sense.(公私分明)
  • Any time you send a message, it’s possible that anything could happen, including reentrancy bugs and so on. Accessing storage directly does not have that problem.(容易引入bug)
  • Sending a message may have performance issues. (Probably not. But it’s possible.)(可能会存在性能问题)

Okay — I don’t fully understand this side of the argument. I did the best I can to explain it, but I’m most definitely in the self.foo camp.

But smart people disagree with me.

Why am I wrong, and why is _foo better?

undefined

转载于:https://www.cnblogs.com/xilifeng/p/4922166.html

变量使用self.foo还是_foo相关推荐

  1. 变量foo 和 bar的由来

    在计算机程序设计与计算机技术的相关文档中,术语foobar是一个常见的无名氏化名,常被作为"伪变量"使用. 单词"foobar"或分离的"foo&qu ...

  2. 转载:编程中常见的Foo,bar是什么意思

    原始链接:编程中常见的Foo,是什么意思.._xhmz的专栏-CSDN博客_foo是什么意思 编程中常见的Foo,是什么意思.. xhmz 于 2014-07-28 13:08:34 发布  4796 ...

  3. 【 Makefile 编程基础之三】详解 Makefile 变量的定义规则使用!

    本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/gcc-makefile/770.html ☞ ...

  4. Makefile完全解析PART5.使用变量

    在Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使用的地方.其与C/C++所不同的是,你可以在Makef ...

  5. HTML5学习笔记(十四):变量作用域

    在JavaScript中,用var申明的变量实际上是有作用域的. 如果一个变量在函数体内部申明,则该变量的作用域为整个函数体,在函数体外不可引用该变量: function foo() {var x = ...

  6. makefile变量赋值

    在定义变量的值时,我们可以使用其它变量来构造变量的值,在Makefile中有两种方式来在用变量定义变量的值. 先看第一种方式,也就是简单的使用"="号,在"=" ...

  7. 【JS笔记】私有变量

    1.任何函数中定义的变量都可以认为是私有变量.函数内部可以访问,外部不能访问. 可以通过闭包创建特权方法访问私有变量. function Foo(){var n=10;this.returnN=fun ...

  8. node.js 函数外定义的变量 函数内赋值后为什么不能带出_(44)python少儿编程之函数(五)--- 作用域...

    函数嵌套 在一个函数内定义了另一个函数 如图,定义了一个outer函数,然后在outer函数里面又定义了一个inner()函数,此时我们只能在outer函数调用inner()函数,如果在outer函数 ...

  9. Java(静态)变量和(静态)代码块的执行顺序

    为什么80%的码农都做不了架构师?>>>    本文讨论Java中(静态)变量.(静态)代码块的执行顺序 首先创建3个类: //1.Foo类,用于打印变量 public class ...

最新文章

  1. 学生管理系统(用maven来导入jar包)
  2. 让小乌龟可以唱歌——对Python turtle进行拓展
  3. 只要工具到位,java也可以很简单
  4. angular路由操作中'#'字符的解决办法
  5. AGC019D - Shift and Flip(枚举)
  6. HTTP方法:幂等性和安全性
  7. 开源软件 | 深度学习用于道路病害检测
  8. Python3基础3——List列表的增删改和内建函数的用法
  9. java 类型转换原理_9.java数据类型的转换
  10. 转数组_Java中IO必杀技-文件转字节数组
  11. Android判断设备是否打开WIFI、GPRS数据连接
  12. WHILE (Transact-SQL)
  13. C#编码应注意的事项
  14. 这份好用视频拼接的软件干货分享,值得收藏
  15. 投影html连接电脑,投影仪怎么连接电脑 电脑与投影仪的连接方法
  16. mysql数据库建表的作用_浅谈(SQL Server)数据库中系统表的作用
  17. 减小App大小:图片篇
  18. 太阳方位角 matlab,太阳天顶角与太阳方位角计算软件V4.1及源码
  19. 使用MVC结构计算梯形面积
  20. c语言printf函数中的格式控制字符串,C++_C语言格式化输入输出函数详解,一:格式输出函数printf() 1 - phpStudy...

热门文章

  1. Distributed Representations of Sentences and Documents
  2. 关于安卓listview获得item中的控件问题
  3. ubuntu火狐证书问题
  4. 汉诺塔(三) 判断操作是否合法 + 栈(stack)数组
  5. Java常用接口与类——main方法/Object类/Scanner类
  6. linux作为生产力工具_适用于Linux的顶级音乐播放器,可保护隐私的社交媒体替代品,打印技巧,生产力工具等
  7. PHPeople如何保持联系并获得乐趣
  8. 文档屏幕水印_您的文档何时需要屏幕截图?
  9. osi7层协议源代码_捍卫开放源代码的17年:立即加入OSI
  10. 前端:JS/35/二级联动菜单,select对象,select对象的属性,option对象,option对象属性,实例:省份列表与城市列表的联动