+ 14

Why can't a variable name start with a number?

15th Jan 2017, 6:47 AM
Akash Verma
Akash Verma - avatar
17 odpowiedzi
+ 35
"Allowing use of variable names that began with a number would probably cause huge problems for the language designers. During source code parsing, whenever a compiler/interpreter encountered a token beginning with a digit where a variable name was expected, it would have to search through a huge, complicated set of rules to determine whether the token was really a variable, or an error. The complexity added to the language parser may not justify this feature."
15th Jan 2017, 6:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 13
I will try to explain this by an example. You need to define what a Number is. Usually you would say everything containing only digits is a Number. This does not contain floating numbers, but lets forget them for now. You can do this with a regular Expression: Number := digit{digit}. {} means it can be repeated 0 to infinite times. 234, 2, 27347748 are all Numbers, but 2Example or asdf234 are not. Now you want Variables to me meaningfull, but you keep it easy and say Variable := (a-z|A-Z) {a-z|A-Z|0-9} ( "|" stands for OR) This is easy, asdf234 is a valid Variable, as is counter or Counter or CoUnTEr12. But look what happens, if you try to have a Variable start with a digit, now: Variable := (a-z|A-Z|0-9) {a-z|A-Z|0-9}, 2Example is a valid Variable, but so is 0983749 or 234524. At the same time a valid Number and a valid Variable. Now you are unable to clearly say which one it is. So you can't parse it and can't translate it accurately. You can define it in a way to not have this Issue, but maybe this forces much more complexity on your parser/compiler for nearly no pay off. It depends on different properties of the programming language. This is why there are languages which allow this behavior, but most don't. In this example saying Variable := {0-9} (a-z|A-Z) {a-z|A-Z|0-9} would make it unique again. Shortly: C++ has rules which would cause conflicts/ destroy uniques of other rules, if you wanted Variables to start with Numbers, therefore it is forbidden. This is what Hatsi Rei wrote, clarified by an example.
16th Jan 2017, 10:33 PM
Ali Mzadeh
Ali Mzadeh - avatar
+ 9
Have you ever heard someone's name begin from number? Like 1Johny, 36Harry? Variable names are similar to human names. As we heard Queen Elizabeth1, Elizabeth2, the variable names could also be var1, var2 etc.
17th Jan 2017, 8:52 AM
Sachin Artani
Sachin Artani - avatar
+ 7
Because the compiler could confuse it with a number literal.
15th Jan 2017, 6:53 AM
Dao
Dao - avatar
+ 2
@Don yes indeed it is an interesting question. so worth upvoting maybe?
16th Jan 2017, 9:39 PM
ifl
ifl - avatar
+ 1
There are lots of problems while parsing/compiling code with such variables. So, from the beginning of our days (1970, lol) that's a gold standart, metasyntax. Just the way it is, face it, like you done with fact that you must walk using your feet, not ears or thumbs. Well, at least until moment when you'll write your own compiler or even language.
17th Jan 2017, 10:50 AM
Алексей БутылкУс
Алексей БутылкУс - avatar
+ 1
because it will stop lazy programmers to start naming all variables form one to infinite!
17th Jan 2017, 2:59 PM
Heresh
Heresh - avatar
0
That's an interesting question...
15th Jan 2017, 6:53 AM
Don
Don - avatar
0
the astric I'd normally a wild card when coding.
17th Jan 2017, 1:23 PM
Shawn Gunnels
Shawn Gunnels - avatar
0
Because every one is not 50cent
18th Jan 2017, 6:29 AM
Akash Tripathi
Akash Tripathi - avatar
0
telling with basic idea you can say that does any noun contains number? a variable is a name you are giving to allocate a memory...as like as an address
25th Jan 2017, 5:39 PM
Shubranil Dev
Shubranil Dev - avatar
0
h
31st Jan 2017, 3:13 PM
dlshad zaxoy
dlshad zaxoy - avatar
0
great question
3rd Feb 2017, 10:36 AM
jad zarif
jad zarif - avatar
0
If you are programmer you don't have to use number or any alse that not understand, so, make your variable be clear and it will be easer for you.
4th Feb 2017, 2:14 PM
Karam Ibrahim
0
it will be better to use Real name that good to be understand.
22nd Feb 2017, 7:53 AM
Karam Ibrahim
0
haha just think about build a string when you combine a variable and numbers like: string result = resultText + 10 if you would write 1resultText the Compiler couldend decide if that is Text or an int. btw never use numbers at all pls not at end and beginning . that can cause difrent problems espacily if you use some older Java compilers.
28th Feb 2017, 7:30 PM
Florian Bässler
Florian Bässler - avatar
0
Variables can be one character length, so let’s take the following example: int 1 = 10; int x = 1 + 5; How much is x? throw new Exception(“I’m so confused!”);
12th Aug 2020, 7:47 PM
علي المنسي
علي المنسي - avatar