What makes a variable name illegal in programming?

Syntax rules, conventions, and readability guidelines that determine illegal variable names when coding.
On this page

What makes a variable name illegal in programming?

Excerpt

This article explains the syntax restrictions, naming conventions, and readability best practices that can make a variable name invalid in languages like Python, JavaScript, C# and more.


When writing code, one of the first steps is naming your variables. But certain variable names are considered illegal or invalid in most programming languages. In this article, we’ll cover the syntax rules, conventions, and readability guidelines that determine what makes a good vs bad variable name.

Introduction

In programming, a variable is a named container that stores data in memory. Variables need to be given unique names that follow the syntax rules of the language you are coding in.

Choosing good variable names is crucial because they make your code more readable and maintainable. Illegal or poorly chosen names can make it hard to understand what your code does.

Syntax Rules

There are certain syntax restrictions that make a variable name invalid:

Illegal Characters

Most languages do not allow spaces, symbols, or special characters in variable names beyond underscores and sometimes dashes. For example, my variable, my#variable, or my$variable would be illegal.

Tools like String_to_Snake_Case help generate valid names that follow these character rules.

Length Restrictions

Languages also limit how long variable names can be, often 255 characters or less. Excessively long names like thisIsAnExtraordinarilyLongVariableName can exceed the length limit.

Reserved Words

Each language has keywords and reserved words that cannot be used as variable names. For example, in Python int, float, return and many others are off limits.

Tools like String_to_Lowercase help avoid accidentally using reserved words.

Stylistic Conventions

Beyond syntax rules, there are also naming convention standards:

Capitalization

Languages often follow capitalization norms like PascalCase or snake_case that can make a name illegal if not followed.

PascalCase vs snake_case

C# uses PascalCase while Python recommends snake_case. Using the wrong style for a language makes a name non-standard.

Tools like String_to_CamelCase help switch between conventions.

Readability

Even when a variable name follows syntax rules, it can still be considered poor form if it is not readable:

Describe Purpose

Names should describe the variable’s purpose or what data it holds. Generic names like data, a, or temp don’t explain what the variable is for.

Avoid Abbreviations

Shortened abbreviations and acronyms in names like ccnum or dept_id can be obscure. Write them out fully for clarity.

Use String_to_Capital Wisely

Some abbreviations like starting a name with Id or Num by convention are OK. But use sparingly and be consistent.

Examples of Illegal Names

Here are some examples of variable names that would be invalid and should be avoided:

Cannot Start with Number

11value = 1 #illegal

Numbers are not allowed at the start of variable names in most languages.

No Spaces Allowed

1int my value = 5; //illegal

Spaces make the name invalid syntax in Java and many other languages.

Avoid Weird Characters and Symbols

1string my$variable! = "value"; //illegal

Special characters like $, !, @ etc. will make the name illegal.

Excessively Long Names

1int thisisaverylongvariablenamethatgoesoverthelimit = 5; //illegal

Long names over the character limit will be truncated or rejected.

Reserved Words Cause Conflict

1var for = 5; //illegal

for is a reserved word in JavaScript so cannot be used as a variable.

Conclusion

There are a few key guidelines to keep in mind for variable naming:

  • Follow language syntax rules for allowed characters, length, and avoiding reserved words
  • Adhere to language conventions like PascalCase and snake_case
  • Use descriptive names that make the meaning clear
  • Avoid unclear abbreviations and acronyms

Tools like String_to_Uppercase help automatically format strings into valid names. But manually considering readability pays off.

Taking the time to give variables clear, legal names makes code more readable and maintainable. Illegal names lead to confusion down the road for you or other developers. So get into good naming habits early on.