Variable Naming Conventions
Variables are examples of identifiers. Identifiers are names given to identify something . There are some rules you have to follow for naming identifiers:
What Are Identifiers?
An identifier is a name you give to something in your code—a variable, function, class, or module. Variables are one type of identifier. When you create a variable, you choose an identifier to represent it. This identifier must follow specific rules set by your programming language.
Every time you write code, you are naming things. These names are identifiers. They serve as labels that help you and other programmers understand what a piece of data or a function does. A well-chosen identifier makes code readable; a poorly chosen one makes it confusing. But before you can worry about whether a name is clear, you must first ensure it follows the language's naming rules.
The Core Rules for Valid Identifiers
Most programming languages enforce a consistent set of rules for what characters and patterns are allowed in an identifier. These rules exist to prevent ambiguity and ensure the language parser can correctly recognize your variable names. While the exact rules vary slightly between languages, the following principles apply across Python, Java, JavaScript, C++, and many others.
- An identifier must begin with a letter (a–z, A–Z) or an underscore (_). It cannot start with a digit.
- After the first character, an identifier may contain letters, digits (0–9), and underscores.
- Identifiers are case-sensitive: myVar, myvar, and MYVAR are three different identifiers.
- Identifiers cannot contain spaces, hyphens, or special characters like @, #, $, %, or &.
- Identifiers cannot be reserved keywords in your language (such as if, for, while, class, def, return, etc.).
Valid vs. Invalid Identifier Names
The valid names on the left all follow the rules: they start with a letter or underscore, contain only letters, digits, and underscores, and are not reserved words. The invalid names on the right each break at least one rule. Notice that 123count fails because it starts with a digit, student-age fails because it contains a hyphen, for fails because it is a reserved keyword, and student age fails because it contains a space.
Naming Convention Styles
Beyond the basic rules, programming communities have adopted naming conventions—consistent patterns for how to write multi-word identifiers. These conventions make code more readable and help developers quickly understand the purpose of a variable. The three most common conventions are snake_case, camelCase, and PascalCase.
- snake_case: Words are separated by underscores, and all letters are lowercase. Python developers strongly prefer this style for variable names. Example: user_email, total_score.
- camelCase: The first word starts with a lowercase letter, and each subsequent word begins with an uppercase letter (no spaces or underscores). JavaScript and Java developers typically use this for variables. Example: userName, totalScore.
- PascalCase: Like camelCase, but the first letter is also uppercase. This style is conventionally used for class names in most languages. Example: UserProfile, StudentRecord.
Follow the naming convention of the language and community you are working in. If you are writing Python, use snake_case for variables and functions, and PascalCase for class names. If you are writing JavaScript, use camelCase for variables and functions, and PascalCase for constructors and classes. Consistency within a project matters more than which convention you choose—pick one and stick with it.
Reserved Keywords and Why They Matter
Every programming language reserves certain words for its own use. These reserved keywords control program flow, define data structures, and manage the language itself. You cannot use them as variable names because the language parser would interpret them as commands, not identifiers. Attempting to do so results in a syntax error.
Common reserved keywords across most languages include: if, else, for, while, return, def, class, import, try, except, pass, break, continue, and, or, not, True, False, None. Each language has its own complete list. When you start learning a new language, it is worth reviewing its reserved keywords so you know what names are off-limits.
Conventions for Private and Public Variables
In object-oriented programming, developers often use naming conventions to signal whether a variable is intended for internal use within a class or available for external use. While these conventions are not enforced by the language itself (except in rare cases), they communicate intent to other programmers.
- Public variables: Named normally (e.g., student_name, age). These are intended to be accessed and modified from outside the class.
- Private variables: Begin with a single underscore (e.g., _internal_count, _cache). By convention, this signals that the variable should only be used within the class or object, not from external code.
- Strongly private variables: Begin with a double underscore (e.g., __secret_key). In some languages like Python, this triggers name mangling, making accidental external access much harder.
Remember that the single underscore prefix is a convention, not a hard rule. Python will not prevent you from accessing a _private_var from outside the class. The underscore is a signal to other developers that they should not rely on or modify that variable directly. Respect this convention in your own code and in code you read.
Common Mistakes in Variable Naming
Starting a variable name with a digit
Identifiers must start with a letter or underscore. The parser cannot distinguish between the number 2 and the start of an identifier.
Fix:
Use second_place = 100 or place_2 = 100 instead.Using spaces or hyphens in variable names
Spaces and hyphens are not allowed in identifiers. The parser treats spaces as separators between tokens, and hyphens are reserved for arithmetic operations.
Fix:
Use student_age = 20 or studentAge = 20 depending on your language's convention.Using a reserved keyword as a variable name
The language interprets for and class as keywords, not variable names, causing a syntax error.
Fix:
Choose a different name: loop_count = 10 or class_name = 'MyClass'.Forgetting that identifiers are case-sensitive
myVar and myvar are two different identifiers. The second line tries to print a variable that was never defined.
Fix:
Use consistent capitalization: either myVar = 5; print(myVar) or myvar = 5; print(myvar).Using special characters in variable names
Special characters like @, $, %, and & are not allowed in identifiers.
Fix:
Use user_email = 'test@example.com' or total_amount = 100.
Choosing Clear and Meaningful Names
Following the naming rules is the minimum requirement. But truly good variable names go further: they are clear, descriptive, and reveal the purpose of the data they hold. A name like x or temp might be technically valid, but it tells you nothing about what the variable represents. A name like student_enrollment_count or user_login_timestamp is far more informative.
- Use descriptive nouns for variables that hold data: user_name, total_price, is_active (for booleans, use is_ or has_ prefix).
- Avoid single-letter names except in very limited contexts (like loop counters: for i in range(10)).
- Avoid abbreviations unless they are universally understood in your domain (e.g., id for identifier, db for database).
- Use full words rather than shortened versions: student_count instead of std_cnt.
- Make the name long enough to be clear, but not so long that it becomes unwieldy.
Practice: Identifying Valid and Invalid Names
For each of the following identifiers, determine whether it is valid or invalid. If it is invalid, explain which rule it breaks. If it is valid, identify which naming convention it follows (snake_case, camelCase, or PascalCase).
Hints
- Remember: identifiers must start with a letter or underscore, can contain letters/digits/underscores only, cannot be reserved keywords, and are case-sensitive.
- If you are unsure whether a word is a reserved keyword, think about whether it is a language control structure or data type.
- Naming conventions are about style, not validity—a name can be valid but follow a different convention than expected in your language.
- user_profile
- _internal_state
- 3rd_attempt
- maxRetries
- my-variable
- UserAccount
- if
- total score
- count_123
- return_value
Summary
Variable naming conventions are the rules and patterns that govern how you write identifiers in code. Mastering them ensures your code is syntactically correct and readable to others. The core rules are universal: start with a letter or underscore, use only letters, digits, and underscores, avoid reserved keywords, and remember that names are case-sensitive. Beyond these rules, adopt the naming convention of your language and community—snake_case for Python, camelCase for JavaScript, PascalCase for class names. Use the single underscore prefix to signal private variables by convention. Most importantly, choose names that are clear and descriptive, making your code self-documenting and easier to maintain.
Key Takeaways
- Identifiers must start with a letter or underscore, contain only letters, digits, and underscores, and cannot be reserved keywords.
- Different programming communities prefer different naming conventions: Python uses snake_case, JavaScript uses camelCase, and most languages use PascalCase for class names.
- Case sensitivity matters—myVar and myvar are two different identifiers.
- Use the single underscore prefix by convention to signal that a variable is intended for internal use only.
- Choose clear, descriptive names that reveal the purpose of the variable, not just technically valid names.