Why do we use _ in variable names?

Programming LanguagesNaming Conventions

Programming Languages Problem Overview


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

Programming Languages Solutions


Solution 1 - Programming Languages

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;
   }
}

Solution 2 - Programming Languages

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.

Solution 3 - Programming Languages

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.

Solution 4 - Programming Languages

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.

Solution 5 - Programming Languages

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;
    }

}

Solution 6 - Programming Languages

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.

Solution 7 - Programming Languages

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

Solution 8 - Programming Languages

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

Solution 9 - Programming Languages

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.

Solution 10 - Programming Languages

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<Employee> 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.

Solution 11 - Programming Languages

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

Solution 12 - Programming Languages

Basically it is telling that the developer should provide the definition . In short it defines it does not have any definition .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeriousTyroView Question on Stackoverflow
Solution 1 - Programming LanguagesAvada KedavraView Answer on Stackoverflow
Solution 2 - Programming LanguagesAndrew ClarkView Answer on Stackoverflow
Solution 3 - Programming LanguagesJon SkeetView Answer on Stackoverflow
Solution 4 - Programming LanguagesmattkellyView Answer on Stackoverflow
Solution 5 - Programming Languagesuser784540View Answer on Stackoverflow
Solution 6 - Programming LanguagesOm.View Answer on Stackoverflow
Solution 7 - Programming LanguagesDaniView Answer on Stackoverflow
Solution 8 - Programming LanguagesMxRView Answer on Stackoverflow
Solution 9 - Programming LanguagesJeremy RomanView Answer on Stackoverflow
Solution 10 - Programming LanguagesJajikanth pydimarlaView Answer on Stackoverflow
Solution 11 - Programming Languagesuser8205152View Answer on Stackoverflow
Solution 12 - Programming LanguagesSantosh MoreView Answer on Stackoverflow