Why don't variable names have spaces?

Related: Why variable names do not start with numbers?

Is there any technical reason why spaces are not allowed in variable names or does this not meet the conditions?

For example, what prevents us from doing something like this?

average score = sum of scores / number of scores 

The only problem that comes to mind is the keywords, but you can simply limit their use in the variable name, and the lexer will be able to distinguish between the variable part and the keyword.

+7
variables programming-languages language-design
source share
3 answers

There is no fundamental reason, except for the decisions of the language developers and the history of single-point identifiers. Some languages โ€‹โ€‹really allow the use of identifiers of several tokens: the expression language MultiMedia Fusions, some software for working with spreadsheets or Mac laptops, whose name eludes me, and I'm sure of others. However, there are several considerations that make the problem non-trivial.

Assuming the language is free form, you need a canonical representation, so that an identifier like account name treated equally regardless of the space. The compiler may need to use some usage conventions to please the linker. Then you should consider the effect of this on external exports - why C ++ has an extern "C" binding specifier to disable mangling.

Keywords are a problem, as you saw. Most C-family languages โ€‹โ€‹have a lexical keyword class that is different from identifiers that are not context-sensitive. You cannot name a class variable in C ++. This can be solved by abandoning keywords in identifiers with several tokens:

 if account age < 13 then child account = true; 

Here, if and then cannot be part of the identifier, so there is no ambiguity with account age and child account . In addition, you can demand punctuation everywhere:

 if (account age < 13) { child account = true; } 

The final option is to make keywords ubiquitously context-sensitive, which leads to monsters like:

 IF IF = THEN THEN ELSE = THEN ELSE THEN = ELSE 

The biggest problem is that matching is an extremely powerful syntax construct, and you don't want to take it easy. Resolving identifiers with multiple tokens prevents matching for other purposes, such as an application or a composite function. I think it is much better to allow most non-white characters and thus allow identifiers like canonical-venomous-frobnicator . Still much readable, but with less scope for ambiguity.

+7
source share

I think this is due to the fact that the language designers followed this convention.

I searched on Google and found that when naming a variable, this is the rule that follows when naming a variable.

Some links are listed below: -

Variable names include the following rules:

  • Variable names cannot contain spaces.

Variable names are case sensitive. The variable name can be any identifier - a sequence of Unicode characters of unlimited length and numbers, starting with a letter, a dollar sign "$" or an underscore "". However, a convention should always begin with your variable names with a letter, not "$" or "". In addition, the dollar sign, by convention, is never used at all. You may find situations in which auto-generated names will contain a dollar sign, but your variable names should always avoid using it. a similar convention exists for the underscore; while it is technically legal to start a variable name with "_", this practice is not encouraged. Free space is not allowed.

In all of the above links, you will find that designers adhered to this naming convention for naming a variable.

Also check if there is any language that allows spaces in the names of its variables

0
source share

This is due to language design. The compiler must figure out the meaning of the words. The compiler works with the "State Machine" method, and it needs to distinguish between keywords. Perhaps placing the variable names in "[" and "]" gives us some solution (for example, SQL). But it will be harder to use in coding ...

0
source share

All Articles