有微不足道的性能永远保存在您的熏肉? [英] Have trivial properties ever saved your bacon?

查看:139
本文介绍了有微不足道的性能永远保存在您的熏肉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多的意见,在那里,你不要暴露你的领域公开,而是用琐碎的性能。我在与放大器看到它;结束了。

There's a lot of advice out there that you shouldn't expose your fields publically, and instead use trivial properties. I see it over & over.

我理解的参数,但<一个href="http://stackoverflow.com/questions/174198/c35-automatic-properties-why-not-access-the-field-directly#205567">I不认为这是个好建议,在大多数情况下,的。

有没有人有时间例子,当它真正重要的?当编写一个简单的财产做重要的事情有可能在未来(或不使用之一,当让他们到真正的麻烦)?

Does anyone have an example of a time when it really mattered? When writing a trivial property made something important possible in the future (or when failing to use one got them in to real trouble)?

编辑:数据绑定的说法是正确的,但不是很有趣。它在数据绑定code,它不会接受公共领域的一个错误。因此,我们必须写性能来解决这个bug,而不是因为属性是一个明智的一流的设计选择。

The DataBinding argument is correct, but not very interesting. It's a bug in the DataBinding code that it won't accept public fields. So, we have to write properties to work around that bug, not because properties are a wise class design choice.

编辑:要清楚,我正在寻找现实世界的例子,而不是理论。一时间,当它真正重要的。

To be clear, I'm looking for real-world examples, not theory. A time when it really mattered.

编辑:设置在二传手断点的能力似乎是有价值的。我设计code调试器是不幸的:我宁愿调试变得更聪明,但鉴于我们的调试器,我会采取这种能力。好东西。

The ability to set a breakpoint on the setter seems valuable. Designing my code for the debugger is unfortunate: I'd rather the debugger get smarter, but given the debugger we have, I'll take this ability. Good stuff.

推荐答案

这可能是很难做出code工作在一个不确定的未来,但是这是没有任何借口偷懒。编码属性过场会议,这是务实的。说它是防御性编程。

It may be hard to make code work in an uncertain future, but that's no excuse to be lazy. Coding a property over a field is convention and it's pragmatic. Call it defensive programming.

其他人也抱怨说,有一个速度问题,但JIT'er是足够聪明,使它几乎一样快,暴露出公共领域。速度不够快,我永远也不会注意到。

Other people will also complain that there's a speed issue, but the JIT'er is smart enough to make it just about as fast as exposing a public field. Fast enough that I'll never notice.

有些浮现在脑海中不平凡的事情

Some non-trivial things that come to mind

  1. 在公共区域是完全公开的,你不能强加只读或只写语义
  2. 属性可以有有不同的 GET 设置辅助功能(如公共获取,内部设置)
  3. 您不能覆盖​​一个领域,但你可以有虚拟财产。
  4. 您的类有在公共场
  5. 无法控制
  6. 您的类可以控制的财产。它可以限制设置为允许范围内的值,标志该状态已更改,甚至延迟加载的值。
  7. 在思考语法的区别。公共领域是不是一个属性。
  8. 在没有数据绑定,因为其他人指出。 (这只是一个错误给你 - 。我能理解为什么.NET框架的设计者不支持的模式,他们是不赞成的)
  9. 您不能把一个接口上的一个字段,但你可以把一个属性的界面上。
  10. 您的财产甚至不需要存储数据。您可以创建一个门面,并派遣一个包含的对象。
  1. A public field is totally public, you can not impose read-only or write-only semantics
  2. A property can have have different get versus set accessibility (e.g. public get, internal set)
  3. You can not override a field, but you can have virtual properties.
  4. Your class has no control over the public field
  5. Your class can control the property. It can limit setting to allowable range of values, flag that the state was changed, and even lazy-load the value.
  6. Reflection semantics differ. A public field is not a property.
  7. No databinding, as others point out. (It's only a bug to you. - I can understand Why .net framework designers do not support patterns they are not in favour of.)
  8. You can not put a field on an interface, but you can put a property on an interface.
  9. Your property doesn't even need to store data. You can create a facade and dispatch to a contained object.

您只键入一个额外的13个字符的正确性。这似乎很难像投机的通用性。有一个语义差异,如果不出意外,一个酒店有不同的语义,是远远超过公共领域更加灵活。

You only type an extra 13 characters for correctness. That hardly seems like speculative generality. There is a semantic difference, and if nothing else, a property has a different semantic meaning and is far more flexible than a public field.

 public string Name { get; set; }
 public string name;

我记得有一次,当第一次使用.NET I codeD几类作为只是字段,然后我需要让他们为某些原因属性,它是完全是浪费时间的时候我能有刚刚做正确的第一次。

I do recall one time when first using .net I coded a few classes as just fields, and then I needed to have them as properties for some reason, and it was a complete waste of time when I could have just done it right the first time.

所以,你对的没有的以下约定是什么原因呢?为什么你觉得有必要去游泳上游?什么是它救了你不这样做呢?

So what reasons do you have for not following convention? Why do you feel the need to swim upstream? What has it saved you by not doing this?

这篇关于有微不足道的性能永远保存在您的熏肉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆