Why do we use _ in variable names?

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

I have seen variables like _ image and was wondering what _ meant?

回答1:

It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:

class Foo {    private int _counter;     public int GetCounter()    {       return _counter;    }     public int SetCounter(int counter)    {       _counter = counter;    } } 


回答2:

In most languages _ is the only character allowed in variable names besides letters and numbers. Here are some common use cases:

  • Separating words: some_variable
  • Private variables start with underscores: _private
  • Adding at the end to distinguish from a built-in name: filter_ (since filter is a built-in function)
  • By itself as an unused variable during looping: [0 for _ in range(n)]

Note that some people really don't like that last use case.



回答3:

Some people use it to indicate that they are variables rather than (say) method names. Or to make it obvious that they're instance variables rather than local variables. Sometimes you see extra prefixes, e.g.

private int m_age; // Member (instance) variable private static int g_maxAge; // Global (static) variable 

It's just a convention. I was going to say "there's nothing magic about _" but that's not quite true - in some languages a double underscore is reserved for "special" uses. (The exact usage depends on the language of course.)

EDIT: Example of the double underscore rule as it applies to C#. From the C# 4 spec, section 2.4.2:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.



回答4:

Usually it separates class fields from the variables. To avoid using this in code constructions.

class MyClass {   private int _myIntField;       private void setMyIntField(int value2Set) {      _myIntField = value2Set;     }  } 


回答5:

The underscore in variable names is completely optional. Many programmers use it to differentiate private variables - so instance variables will typically have an underscore prepended to the name. This prevents confusion with local variables.



回答6:

Well Underscore character(_) begin with your variable name is discouraged but it is legal and some people use it to identify as an private variable and some for naming it in caching variable. Go through with this link too.



回答7:

_ usually means something private or internal. In C++ standard libraries all implementation specific variables must start with _.



回答8:

In most languages, it doesn't actually affect the functionality of the code, but is often used to denote reserved or internal names.

It is common in some languages to name your instance variable _image or image_ and then make the public method used to access it image().

Similarly, some names like __FILE__ are used in some languages to denote a special variable or constant created by the interpreter or compiler; such names are often reserved to encourage programmers to avoid using those names in their own programs in case the names are used in future versions of the language.



回答9:

To avoid reserved keywords, or in reserved keywords, making them more easily avoided. A single underscore is discouraged and reserved in JavaSE9.



回答10:

Another use case (mainly in javascript) is when you need to assign the current instance this to a local variable we write as below

var _this = this; 

If you need to create a local temporary object reference, to differentiate between the actual needed reference, we create as below

List employeeList = new ArrayList(); for (Employee _employee : employeeList) {} 

So if we follow this best practice, every time you see a variable with _ , we come to a conclusion that its being used to solve some business need at that particular method.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!