Why can not I declare a variable with a number in front?

6

I'm curious to know why not being able to create variables with a number up front. 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 that PHP, for example, allows special characters, but maybe this is because of the initial identifier, which is a $ .

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

    
asked by anonymous 29.07.2015 / 15:43

2 answers

10

There is the rule because it is ambiguous. If the variable starts with a number the compiler does not know that it is a symbol (a variable, for example). He will find it to be a numeric literal.

Notice that until now 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 it is not a literal, but then you would have trouble identifying).

This is especially tricky when literals can have letters at the end to differentiate the type. If this did not exist in the language, it would even give the compiler a chance to figure out what it really is. It is true that this still exists, but only in certain cases. It's better not even try this. The gain would be minimal and would deal with other confusions.

Names with numbers only is not possible under any circumstances, could not differentiate.

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 escape character in this case, as it is possible to use @ in C #, for example. But again, the gain does not compensate the effort, it gives to live without variables that start with numbers. Note however that C # allows the use of @ only to escape keywords but not names beginning 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 give a relationship information with column in the DB.

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)

No way to know, it's ambiguous.

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

Let's say that one day the language needs to do a literal that is determined by the teste suffix. Okay, 4teste would already be ambiguous. Of course, you may find that teste is a bad suffix, but a compiler can not do value judgment. And the language could create a mechanism that the programmer creates his own suffixes and it could be teste or something else. If there were a variable with a number and the same name, it would complicate. 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
    
29.07.2015 / 15:51
5

A variable should not start with a number because in this way the compiler / interpreter will not be able to differentiate whether it is a value ex: 300 or if it is a variable, in addition to the confusion to read the code.

var 300 = 10;

if(300 > 500){ ???
}

Some database allow you to use special characters or numbers at the beginning of the names because they are escaped, mysql is used for this.

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

Related: Special Characters in PHP Identifiers

    
29.07.2015 / 15:46