Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use the dollar symbol ($) in a variable name?

The dollar symbol ($) is a valid character to name a variable, e.g. String superSecretFormula$;, but when we're talking about naming conventions, when should I use this symbol?

Underscore for example is mostly used to separate words, as blank spaces can't be used.

like image 576
Zignd Avatar asked Dec 27 '12 15:12

Zignd


People also ask

Can dollar sign be used in variable name?

Variable names can be up to 100 characters long, not including the dollar sign ($). Examples of variable names are $docname and $_currentline. Although a variable must start with a letter or underscore (_), the remainder of the name can consist of letters, digits, and underscores.

What do the dollar ($) signs signify?

It stands for the initials of the United States'.

Why do we use a dollar symbol ($) before variables in PHP?

Rasmus Lerdorf, the father of the PHP language, explains the $ sign as an ability to insert variables inside literal string values (interpolation), so that the variables are distinguished from the rest of the string.

Can you use a dollar sign in a variable name Java?

"Java does allow the dollar sign symbol $ to appear in an identifier, but these identifiers have a special meaning, so you should not use the $ symbol in your identifiers."

Can I use the dollar sign in a variable name?

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.

What does the dollar sign mean in PHP?

A dollar sign in php acts like the dollar sign in bash, it refers to a variable, in bash you can use the dollar sign with commands to get their output and assign them to a variable like this:

What does the $ symbol before a variable name mean?

The leading $ symbol before a variable name is called a sigil. Its purpose is to make it clear that the name following the sigil is a variable and not something else, like a function name or a constant or a keyword. Sigils remove ambiguity to make the programming language interpreter's job easier.

Can the dollar sign be used for identifiers in Java?

Java allows the dollar sign to begin identifiers, but recommends that it only be used for mechanically generated code. (see here) It appears that in C++ identifiers may be able to use dollar signs as an extension, but it's not part of the standard.


2 Answers

From the Java Language Specification on identifiers:

The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

like image 179
McDowell Avatar answered Oct 14 '22 07:10

McDowell


Your question is tagged Java, but it looks like you're asking about type characters, which are still supported in Visual Basic but not widely used these days (nor for a long time).

This is a bit subjective, but I think it's fair to say: never

One scenario where you might want to prefix a variable name with $ is when writing JavaScript code to distinguish jQuery objects.

Edit

Regarding starting a variable name with a $, the Oracle Java tutorials tell us:

A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin your variable names with a letter, not "$" or "_".

like image 22
Richard Ev Avatar answered Oct 14 '22 07:10

Richard Ev