有什么理由不使用“this”吗? (“自我”,“我”,...)?
我读了 这个答案 及其评论,我很好奇:不使用 this
/ Self
/ Me
是否有任何原因?
顺便说一句:如果之前有人问过这个问题,我很抱歉,似乎不可能在 SO 上搜索这个词 this
。
I read this answer and its comments and I'm curious: Are there any reasons for not using this
/ Self
/ Me
?
BTW: I'm sorry if this has been asked before, it seems that it is impossible to search for the word this
on SO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
警告:以下纯粹主观答案。
我认为不使用 this/self/me 的最佳“原因”是简洁。 如果它已经是成员变量/函数那么为什么要多余地添加前缀呢?
就我个人而言,我避免使用 this/self/me ,除非有必要为编译器消除特定表达式的歧义。 许多人不同意这一点,但在我工作过的任何团队中,这都不是真正的症结所在。
Warning: Purely subjective answer below.
I think the best "reason" for not using this/self/me is brevity. If it's already a member variable/function then why redundantly add the prefix?
Personally I avoid the use of this/self/me unless it's necessary to disambiguate a particular expression for the compiler. Many people disagree with this but I haven't ever had it be a real sticking point in any group I've worked for.
我认为大多数常见场景已经在已经引用的两篇文章中涵盖了; 主要是简洁和冗余与清晰 - 一个小的补充:在 C# 中,需要使用“this”才能访问当前类型的“扩展方法” - 即
声明
Foo()
的位置外部为:I think most of the common scenarios have been covered in the two posts already cited; mainly brevity and redundancy vs clarity - a minor addition: in C#, it is required to use "this" in order to access an "extension method" for the current type - i.e.
where
Foo()
is declared externally as:它在某些情况下澄清了这一点,例如 c# 中的示例:
It clarifies in some instances, like this example in c#:
如果您在启用所有规则的情况下使用 StyleCop,它会让您将
this.
放入。自从我开始使用它以来,我发现我的代码更具可读性,但这是个人偏好。If you use StyleCop with all the rules on, it makes you put the
this.
in. Since I started using it I find my code is more readable, but that's personal preference.我认为这不是问题,因为它只会增加代码的可读性,这是一件好事。
对于某些语言,例如 PHP,甚至必须添加 $this-> 前缀。 如果您需要使用类字段或方法。
我不喜欢这样的事实:如果 PHP 有某种方法可以在没有它的情况下引用类成员,它会使某些行不必要地比实际长度长。
I think this is a non-issue, because it only adds more readability to the code which is a good thing.
For some languages, like PHP, it is even mandatory to prefix with $this-> if you need to use class fields or methods.
I don't like the fact that it makes some lines unnecessarily longer than they could be, if PHP had some way to reference class members without it.
我个人发现
this.whatever
的可读性较差。 您可能没有注意到 2 行方法中的差异,但是等到您在类中的任何地方都得到this.variable
和this.othervariable
时。此外,我认为使用
this.
被发现可以替代令人讨厌的匈牙利表示法的一部分。 有些人发现,读者仍然可以更清楚地看出变量是类成员,而this.
做到了这一点。 但是,如果我们需要额外的清晰度,为什么要欺骗自己而不使用普通的旧"m_"
或简单的"_"
呢? 这是 5 个字符对 2 个(甚至 1 个)字符。 更少的打字,同样的结果。话虽如此,风格的选择仍然是个人喜好的问题。 很难说服习惯以某种方式阅读代码的人对更改代码有用。
I personally find that
this.whatever
is less readable. You may not notice the difference in a 2-line method, but wait until you getthis.variable
andthis.othervariable
everywhere in a class.Furthermore, I think that use of
this.
was found as a replacement for a part of the much hated Hungarian notation. Some people out there found out that it's still clearer for the reader to see that a variable is a class member, andthis.
did the trick. But why fool ourselves and not use the plain old"m_"
or simply"_"
for that, if we need the extra clarity? It's 5 characters vs. 2 (or even 1). Less typing, same result.Having said that, the choice of style is still a matter of personal preference. It's hard to convince somebody used to read code in a certain way that is useful to change it.
好吧,Eclipse 用不同的颜色来区分字段、参数和局部变量,因此至少在 Eclipse 环境中工作时不需要在语法上区分字段,以便为自己和后代专门将它们标记为“字段”。
well, eclipse does color fields, arguments and local variables in different colors, so at least working in eclipse environment there is no need to syntactically distinguish fields in order to specially mark them as "fields" for yourself and generations to come.
之前确实有人问过,在“java中的变量”上下文中:
Do you在java中为实例变量添加“this”前缀吗?
主要原因似乎是:
可读性,换句话说......我不买这个,我发现
this.
非常有用。It was asked before indeed, in the "variable in java" context:
Do you prefix your instance variable with ‘this’ in java ?
The main recurrent reason seems to be:
Readability, in other word... which I do not buy, I find
this.
very useful.这对我来说听起来像是无稽之谈。 使用“this”可以使代码变得更好,而且我认为它没有任何问题。 这样的政策是愚蠢的(至少当你甚至不告诉人们为什么制定这些政策时)。
That sounds like nonsense to me. Using 'this' can make the code nicer, and I can see no problems with it. Policies like that is stupid (at least when you don't even tell people why they are in place).
对我来说,我使用
this
来调用实例化对象的方法,而self
用于静态方法as for me i use
this
to call methods of an instantiated object whereasself
is for a static method在 VB.NET 中,我使用的常见做法之一是以下代码:
并非总是如此,但大多数情况下 Me / this / self 非常有用。 澄清您正在谈论的范围。
In VB.NET one of the common practice I use is the following code :
Not all the time but mostly Me / this / self is quite useful. Clarifies the scope that you are talking.
在典型的 setter 方法中(取自 lagerdalek 的答案):
如果您没有使用它,编译器将不知道您正在引用成员变量。
this.
的使用是告诉编译器您需要访问一个成员变量 - 该变量超出了该方法的直接范围。 在方法中创建与成员变量同名的变量是完全合法的,就像覆盖扩展了另一个类的类中的方法是完全合法的一样。但是,如果您仍然需要使用超类的方法,则可以使用
super。
在我看来使用这个。 并不比使用 super 差。 并允许程序员在代码中具有更大的灵活性。就我而言,可读性甚至不涉及其中,这完全取决于变量的可访问性。
In a typical setter method (taken from lagerdalek's answer):
If you didn't use it, the compiler wouldn't know you were referring to the member variable.
The use of
this.
is to tell the compiler that you need to access a member variable - which is out of the immediate scope of the method. Creating a variable within a method which is the same name as a member variable is perfectly legal, just like overriding a method in a class which has extended another class is perfectly legal.However, if you still need to use the super class's method, you use
super.
In my opinion using this. is no worse than using super. and allows the programmer more flexibility in their code.As far as I'm concerned readability doesn't even come into it, it's all about accessibility of your variables.
最终,这始终是个人选择的问题。 就我个人而言,我使用这种编码约定:
所以对我来说,“this”实际上是保持构造函数可读的必要条件。
编辑:当我编写上面的代码时,“sinje”发布了完全相同的示例。
In the end it's always a matter of personal choice. Personally, I use this coding convention:
So for me "this" is actually necessary to keep the constructor readable.
Edit: the exact same example has been posted by "sinje" while I was writing the code above.
我不仅经常使用“这个”。 我有时会用“那个”。
等等。 我的代码中的“That”通常表示同一类的另一个实例。
Not only do I frequently use "this". I sometimes use "that".
And so on. "That" in my code usually means another instance of the same class.
'这。' 在代码中总是向我暗示编码员已经使用了智能感知(或其他 IDE 等效项)来完成繁重的工作。
我当然对此感到内疚,但出于纯粹的虚荣原因,我后来将它们删除了。
我使用它们的唯一其他原因是限定不明确的变量(不好的做法)或构建扩展
方法
'this.' in code always suggests to me that the coder has used intellisense (or other IDE equivalents) to do their heavy lifting.
I am certainly guilty of this, however I do, for purely vanity reasons, remove them afterwards.
The only other reasons I use them are to qualify an ambiguous variable (bad practice) or build an extension method
Qualifying a variable