Why can’t I declare a variable with a numeric before the name?

Asked

Viewed 1,999 times

8

I’m curious to know why I can’t create variables with a number before the name. I did some tests in Javascript, Actionscript and C#.

var 4teste:String = "teste"; //ActionScript, erro!
var 4teste = "teste"; //Javascript, erro!
String 4teste = "teste"; //C#, erro!

var teste4:String = "teste"; //ActionScript, ok!
var teste4 = "teste"; //Javascript, ok!
String teste4 = "teste"; //C#, ok!

I know PHP, for example, allows special characters, but maybe this is because of the initial identifier, which is a dollar sign $.

Is there any 'rule' in languages that does not allow the use of numbers at the beginning of a variable?

2 answers

15


There is the rule because it is ambiguous. If the variable starts with a number the compiler does not know it is a symbol (a variable, for example). He’ll think it’s a numerical literal.

Note that even here on the site when you use the number starting the name, the colorization thinks it is a literal (it is true that in these specific cases you can know that is not a literal, but then would have trouble identifying).

This is especially complicated when literals can have letters at the end to differentiate the type. If this did not exist in the language, it would even be possible for the compiler to get away with it and find out what it really is. It is true that it still gives this by existing but only in certain cases. It is better not to try this. The gain would be tiny and would treat other confusions.

Names only with numbers is not possible under any circumstances, would not be able to differentiate. Okay, I could do a Gambi, when you’re declaring (if the syntax makes it clear that it is a statement it could accept as being an identifier rather than a number, then every time it finds it in the code it will consider it to be the identifier and it cannot in that code have that literal number being used. But why all the crazy?

PHP may be more permissive with some symbols since the language requires a specific character to indicate that it is a variable.

Some languages could do this with an optional character of escape in this case, how is it possible to use the @ in C#, for example. But again, the gain does not make up for the effort, you can live without variables that start with numbers. Note however that C# allows the use of @ just to escape keywords but not names started with numbers.

If you need a name like this because it comes from a database that accepts this type of name, it is possible in C# to use an attribute to name better, not the variable, but at least to give a relation information to the column in BD.

var 2d = 1;
WriteLine(2d); //está imprimindo "1" (o valor de 2d), ou imprimindo "2.0" que é um double)
var 3 = 1;
WriteLine(3); //está imprimindo "1" (o valor de 3), ou imprimindo "3" que é um int)

There’s no way to know, it’s ambiguous.

In his example 4teste in thesis may not be ambiguous, but why make this exception? The gain does not pay. And if you had this, could limit the expansion of language.

Let’s say that one day the language needs to make a literal that is determined by the suffix teste. Ready, 4teste would already be ambiguous. Of course you might find that teste It’s a lousy suffix, but a compiler can’t judge. And language could create a mechanism that the programmer creates his own suffixes and he could be teste or something else. If there was a variable with a number and the same name, it would complicate it. Example:

var 5dias = 1;
//um dia uma pessoa cria o sufixo dias, aí seria permitido:
var x = 5dias; //antes saberia que está guardando 1 em x, agora pode estar guardando 5 dias

I put in the Github for future reference.

  • Great explanation. It’s very confusing, the interpreter treats the letters after the numeric character as what exactly? Or, in any case, when there is an initial numeric character, it would treat as a type "Number", "int", "Double", etc., that’s it?

  • Depending on the language, C# would only treat numbers as int, if you have a suffix d would be double, if the suffix is L would be long, m would be decimal, and so it goes. If it is a letter or word not accepted by the language, it would be wrong. JS would treat as Number.

  • +1 for the double example.

6

A variable should not start with a number because that way the compiler/interpreter will not know if it is an ex: 300 value or if it is a variable, besides the confusion to read the code.

var 300 = 10;

if(300 > 500){ ???
}

Some databases allow using special characters or numbers at the beginning of the names because they are escaped, in mysql use the crase for this.

CREATE TABLE `test`.`300` (
  `id300` INT NOT NULL COMMENT '',
  PRIMARY KEY (`id300`)  COMMENT '');

Related: Special characters in PHP identifiers

  • But when using the reporting operator var before the name, the compiler/interpreter would no longer know that you are declaring a variable?

  • @biio, and in if how will he know if the 300 is really 300 or if it is 10.

  • Got it! I agree on a variable with only numeric characters, but would that occur in a variable called "4test"? The interpreter would recognize this variable being what exactly, a numerical type as well?

  • 1

    @biio, 4teste will be identified as an escaped string.

  • 3

    @If the variables could begin with numbers, it could give rise to very confusing names, such as 187275675a (note the a at the end). Or worse, 129826O (the last character is a o, and not a zero). By making it mandatory for the first letter to be a letter, these problems no longer exist - so by the first character we can easily tell if it is the name of a variable or a literal number: a12345 is a variable, and 12345 is a number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.