Is there nothing wrong with digits in identifier name?

纵饮孤独 提交于 2019-12-13 08:59:47

问题


Since in C\C++\Java -

int 2a ; //invalid suffix "a" on integer constant

Is there nothing wrong with digits in the rest of variant name although it's valid syntax ?

Like -

int num1 ;
int num_1st ;
int num_2nd ; 

回答1:


I've never heard of anybody (e.g., any coding standards/guidelines) that had a problem with digits in an identifier. Nonetheless, too many of them can indicate that a vector or array might be preferable -- even with only two, your num_1st and num_2nd might be better as numbers[2].




回答2:


An identifier cannot start with a number. It must start with a letter or an underscore.




回答3:


Variables identifier must start with a letter or an underscore, but the rest of the characters can be letter, underscore or a digit.

You can even decalre a variable: int _ = 0;

Or if you are familiar with regular expression, it can be patterned as: "[a-zA-Z_]\w*?\b"

Where the \w*? part is not a must.




回答4:


Answer is, no there is nothing wrong with numbers in the rest of the identifier name.




回答5:


As long as variable names are meaningful, using digits as part of the name is definitely not a bad thing. Of course, having a large number of similarly named variables with just a number at the end to differentiate them could be a sign of bad design.

The reason for nor allowing identifiers starting with digits, I'm pretty sure, is that it makes it so much easier to write the parser:

c = getchar(); 
ungetc(c);
if (isdigit(c)) 
   token = number(); 
else 
   token = identifier(); 



回答6:


This is the rule off identifier that it must start with an letter or an underscore after that only digits are allowed.



来源:https://stackoverflow.com/questions/13977739/is-there-nothing-wrong-with-digits-in-identifier-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!