In programming, variable names are essential for storing and manipulating data. However, not all names can be used as variables.
Each programming language has specific rules that determine what constitutes a legal variable name. Understanding these rules is crucial for writing syntactically correct and bug-free code.
This article explores the concept of legal and illegal variable names, the typical rules governing them, and examples that illustrate which names are valid and which are not. By the end, you will be able to confidently identify illegal variable names in most programming languages.
What Is a Variable Name?
A variable name is an identifier used to reference stored data in a program. When you declare a variable, you assign a name to a memory location where data is saved.
This name allows you to retrieve or modify the value stored there later in the code.
Variable names must follow specific syntax rules defined by the programming language. For example, in languages like Python, Java, C, and JavaScript, variable names are subject to naming conventions and restrictions.
These rules ensure that variable names do not conflict with language keywords or cause ambiguity during compilation or interpretation.
Common Rules for Legal Variable Names
Although the exact rules vary slightly depending on the programming language, there are several common conventions that apply broadly:
- Start with a letter or underscore: Variable names usually must begin with a letter (a-z, A-Z) or an underscore (
_). - Followed by letters, digits, or underscores: After the first character, digits (0-9) are typically allowed along with letters and underscores.
- No spaces: Variable names cannot contain spaces.
- Case sensitivity: Many languages treat variable names as case-sensitive, so
Variableandvariableare different. - Cannot be a reserved keyword: Variable names cannot be the same as language keywords like
if,while, orreturn. - No special characters: Symbols like
@,$,!,%are generally not allowed (some languages allow exceptions).
Let’s summarize these in a table for clarity:
| Rule | Description | Example |
|---|---|---|
| Start Character | Must be a letter or underscore | myVar, _count |
| Subsequent Characters | Can be letters, digits, or underscores | var1, data_2 |
| No Spaces | Spaces not allowed in variable names | my var (illegal) |
| Case Sensitivity | Uppercase and lowercase letters differ | Var and var are distinct |
| No Reserved Words | Cannot use language keywords | if, while (illegal) |
| No Special Characters | Symbols like @, ! disallowed |
price$, count! (illegal) |
Examples of Legal and Illegal Variable Names
Examining specific examples helps to reinforce the rules. Below are some common cases with explanations:
| Variable Name | Legal? | Reason |
|---|---|---|
myVar |
Yes | Starts with a letter, contains only letters |
_temp |
Yes | Starts with an underscore, allowed |
2ndValue |
No | Starts with a digit, which is not allowed |
total_amount |
Yes | Letters and underscore, valid |
price$ |
No | Contains a special character $, illegal in many languages |
var name |
No | Contains space, not allowed |
for |
No | Reserved keyword in many languages |
userName1 |
Yes | Valid letters and digits |
_123 |
Yes | Starts with underscore, followed by digits |
!error |
No | Starts with special character ! |
Why Are Some Variable Names Illegal?
Programming languages enforce rules on variable names to avoid confusion and parsing errors. For example, starting a variable name with a digit can confuse the compiler or interpreter, as it may interpret it as a number rather than an identifier.
Reserved keywords are illegal as variable names because they have predefined meanings in the language syntax. Using them as variable names would cause ambiguity in the code and make it harder to understand or execute.
Special characters are often disallowed because they serve as operators or part of syntax. Including them in variable names could create parsing conflicts or unexpected behavior.
“Choosing clear and legal variable names is fundamental to writing maintainable and error-free code.”
Language-Specific Notes
While the general rules are similar, some programming languages have unique allowances or restrictions for variable names. Here are a few examples:
Python
Python variable names:
- Must start with a letter (a-z or A-Z) or underscore (
_). - Can contain letters, digits, and underscores after the first character.
- Cannot be a Python keyword (e.g.,
def,class,if). - Case-sensitive.
Example:
| Variable | Valid? | Note |
|---|---|---|
_var |
Yes | Legal |
var2 |
Yes | Legal |
2var |
No | Starts with digit |
class |
No | Reserved keyword |
Java
In Java, variable names:
- Must start with a letter, underscore (
_), or dollar sign ($) — although the dollar sign is generally discouraged. - Can be followed by letters, digits, underscores, or dollar signs.
- Cannot be a reserved keyword.
- Are case-sensitive.
Example legal variable names in Java: myVar, _temp, $value.
JavaScript
JavaScript variable names follow similar rules:
- Must start with a letter, underscore, or dollar sign.
- Subsequent characters can be letters, digits, underscores, or dollar signs.
- Cannot be a reserved keyword.
- Case-sensitive.
Example:
| Variable | Legal? | Comment |
|---|---|---|
$value |
Yes | Dollar sign allowed |
_value |
Yes | Underscore allowed |
123value |
No | Starts with digit, illegal |
var |
No | Reserved word |
How to Identify Illegal Variable Names?
When faced with a variable name and asked, “Which one is not a legal variable name?” follow a systematic approach:
- Check if the name starts with a valid character (letter, underscore, or language-specific allowed symbol).
- Verify that the rest of the name contains only allowed characters (letters, digits, underscores, etc.).
- Ensure the name does not contain spaces or special characters that are disallowed.
- Confirm the name is not a reserved keyword in the programming language.
- Check for case sensitivity if relevant.
By applying these steps, you can quickly eliminate illegal options.
Example Question
Consider the following variable names in Python. Which one is not legal?
data_1_temp1stValuetotalSum
Answer: 1stValue is not a legal variable name because it starts with a digit.
Special Cases and Exceptions
Some programming languages allow certain special characters that others do not. For instance, PHP allows variable names to start with a dollar sign ($), e.g., $var.
Similarly, Perl also follows this convention.
Unicode characters in variable names are supported in some modern languages like Python 3, allowing letters from non-English alphabets. For example, 变量 is a legal variable name in Python 3.
However, most languages do not allow spaces or punctuation marks in variable names. Using such characters will typically result in syntax errors.
Summary Table: Legal vs. Illegal Variable Names
| Variable Name | Legal? | Reason |
|---|---|---|
name |
Yes | Starts with letter, simple and valid |
_name |
Yes | Starts with underscore, allowed |
name1 |
Yes | Letters followed by digits |
1name |
No | Starts with digit |
name! |
No | Contains special character ! |
name name |
No | Contains space |
for |
No | Reserved keyword |
$name |
Depends | Allowed in some languages (JavaScript, PHP), disallowed in others |
变量 |
Yes | Allowed in Python 3 and some languages supporting Unicode |
Best Practices for Naming Variables
Legal variable names are just one part of writing good code. Following naming conventions and best practices improves code readability and maintainability.
- Use meaningful names: Names like
count,totalPrice, oruserNameclearly describe the variable’s purpose. - Follow style guides: For example, in Python, use snake_case (
total_count), while in Java or JavaScript, camelCase (totalCount) is common. - Avoid single-letter names: Except for counters or iterators, use descriptive names.
- Do not use reserved keywords: This avoids syntax errors and confusion.
- Be consistent: Stick to one naming convention throughout your project.
“Good variable names are the foundation of understandable and maintainable code.”
Conclusion
Determining which variable name is not legal requires understanding the syntax rules of the programming language you are using. The most common reasons for illegal variable names include starting with digits, using spaces or special characters, or using reserved keywords.
When asked “Which one is not a legal variable name?”, apply the fundamental rules outlined above. Always remember that beyond legality, choosing meaningful and consistent variable names enhances code quality significantly.
Keep practicing by reviewing code snippets, identifying illegal names, and correcting them. This will build your confidence and proficiency in all programming languages.