Why can't variable names have spaces in them? [duplicate]
Asked Answered
O

3

9

Related: Why can't variable names start with numbers?

Is there a technical reason why spaces aren't allowed in variable names or is it down to convention?

For example, what's stopping us from doing something like this?:

average score = sum of scores / number of scores

The only issue that comes to mind is keywords, but one could simply restrict the use of them in a variable name, and the lexer would be able to distinguish between part of a variable and a keyword.

Oedipus answered 25/12, 2013 at 5:48 Comment(4)
Not all languages require parentheses for function calls so average score looks like average(score) in some languages.Ghassan
@TedHopp: That is asking for a language with the feature. This is asking for a technical explanation of why the feature is not common. I think it might be better suited to Programmers.Sanjak
@JonPurdy should I move this over to Programmers then?Oedipus
@Volatility: You can flag it and ask for a moderator to migrate/merge it. I just realised that it has been asked on Programmers before, and I even answered that question! :(Sanjak
S
10

There’s no fundamental reason, apart from the decisions of language designers and a history of single-token identifiers. Some languages in fact do allow multi-token identifiers: MultiMedia Fusion’s expression language, some Mac spreadsheet/notebook software whose name escapes me, and I’m sure of others. There are several considerations that make the problem nontrivial, though.

Presuming the language is free-form, you need a canonical representation, so that an identifier like account name is treated the same regardless of whitespace. A compiler would probably need to use some mangling convention to please a linker. Then you have to consider the effect of that on foreign exports—why C++ has the extern "C" linkage specifier to disable mangling.

Keywords are an issue, as you have seen. Most C-family languages have a lexical class of keywords distinct from identifiers, which are not context-sensitive. You cannot name a variable class in C++. This can be solved by disallowing keywords in multi-token identifiers:

if account age < 13 then child account = true;

Here, if and then cannot be part of an identifier, so there is no ambiguity with account age and child account. Alternatively, you can require punctuation everywhere:

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

The last option is to make keywords pervasively context-sensitive, leading to such monstrosities as:

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

The biggest issue is that juxtaposition is an extremely powerful syntactic construct, and you don’t want to occupy it lightly. Allowing multi-token identifiers prevents using juxtaposition for another purpose, such as function application or composition. Far better, I think, just to allow most nonwhitespace characters and thereby permit such identifiers as canonical-venomous-frobnicator. Still plenty readable but with fewer opportunities for ambiguity.

Sanjak answered 25/12, 2013 at 6:12 Comment(1)
Oh well, looks like this isn't going to be migrated. But thanks for the great answer, especially the part about juxtaposition. Having coming from a mainly Python background, it didn't come to mind.Oedipus
U
0

I think it is bacause the designers of the language have followed this convention.

I have searched on Google and found that while naming a variable this is a rule which is followed while naming a variable.

Some links are given below:-

The following rules apply to variable names:

  • Variable names cannot contain spaces.

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

In all of the above links you will find that the designers have followed this naming convention for naming the variable.

Also check Is there any language that allows spaces in its variable names

Unashamed answered 25/12, 2013 at 6:3 Comment(0)
B
0

This is forced from language designing. Compiler needs to find out the meaning of words. Compiler works on a "State Machine" method, and it needs to distinguish key words. Maybe placing variable names in "[" and "]" give us some solution(like SQL). But it will be harder to use it in coding...

Barren answered 25/12, 2013 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.