49

C# style suggests using CamelCase in identifiers to delimit words. Lisp tradition suggests using-dashes-instead.

Has there ever existed a programming language where using spaces in identifiers was not only allowed, but a commonly used idiom when employing multi-word identifiers?

It's possible to have identifiers with spaces in some Scheme implementations, but it's not a widely seen practice. Here's an example:

Petite Chez Scheme Version 8.4
Copyright (c) 1985-2011 Cadence Research Systems

> (define |hey there| 100)
> (define |x y z| 200)
> (list |hey there| |x y z|)
(100 200)
Peter Mortensen
  • 1,050
  • 2
  • 12
  • 14
dharmatech
  • 531
  • 5
  • 11
  • If you have namespaces, that is a form of compound identifier. E.g. C++: `bobs_utilities :: string_functions :: scramble`. This is a name, and we can include arbitrary whitespace if we want because it is syntax, an not a simple token. Names with multiple components want to be abstract syntax; shoehorning namespace info into a single identifier is basically a "name mangling" hack for representing structure inside text where you are lacking the mechanism to represent the structure. – Kaz Apr 24 '12 at 18:25
  • Pretty common in JS, whose primary author was a Scheme guy. – Erik Reppen Jun 14 '13 at 23:58
  • 1
    @ErikReppen As far as I know, spaces are not valid as part of javascript identifiers... – Izkata Jun 15 '13 at 04:58
  • Not for vars no. For property names we can use any string in brackets. e.g. `alert({'some Prop':'bob'}['some Prop']);` but if those string property names fail the identifier/label test you can't use them with dot notation. – Erik Reppen Jun 15 '13 at 06:08
  • In Ruby you can: `define_singleton_method "sjdlkfjsljk#$SDEF SDFSDF@# @#$!!~" do; puts 42; end;` and then you can: `send "sjdlkfjsljk#$SDEF SDFSDF@# @#$!!~"` but it is not common. – Darek Nędza Apr 04 '14 at 08:15
  • Some dialects of BASIC have multi-keyword command names. Not the same thing, of course, but considering these are built-in commands in dialects with huge numbers of commands that should really be in libraries, they're related in a cheating kind of way. Anyway, examples include game-oriented BASIC dialects old and new (Dark BASIC, IIRC STOS on the Atari ST) and I think even the original Dartmouth BASIC (for matrix operations). Of course two or more keywords making a command name is trivial provided the parser recognizes the commands. –  Aug 15 '14 at 14:14
  • Another language which allows spaces is Windows BAT file language. – vbezhenar Jul 15 '19 at 23:44
  • Have a look at https://github.com/featurist/pogoscript – Jerry Jeremiah May 02 '20 at 08:56

22 Answers22

79

FORTRAN compilers ignored spaces so:

   result = value * factor  
   r e s u l t = val ue * fac tor
   result=value*factor

were identical as far as the compiler was concerned.

Some SQL dialects allow embedded spaces in column names but they need to be surrounded by backquotes or some other delimiter before they can be used.

Emily
  • 101
  • 2
James Anderson
  • 18,049
  • 1
  • 42
  • 72
  • 7
    +1, this is new to me. I always wondered why I only got a B in Fortran but now I know :) – NoChance Apr 24 '12 at 05:44
  • 39
    Sun's FORTRAN manual used to include this sentence: "Consistently separating words by spaces became a general custom about the tenth century A.D., and lasted until about 1957, when FORTRAN abandoned the practice." – Blrfl Jun 14 '13 at 21:52
29

Visual Basic (and VBScript) also allow whitespace in identifiers if you surround the identifier with square brackets.

Dim [Hello World]
[Hello World] = 123

However, doing so is quite rare.

Eric Lippert
  • 45,799
  • 22
  • 87
  • 126
18

Does SQL count?

create table "Registered Members" (
    "Full Name" varchar(100),
    "Mailing Address" varchar(100),
    etc...
);
Barry Brown
  • 4,095
  • 4
  • 25
  • 27
18

Inform 7 is a system for developing interactive fiction using natural language–like syntax, in which multi-word identifiers are commonplace:

Mr Jones wears a top hat. The crate contains a croquet mallet. 

The restriction, of course, is that an identifier can’t contain a keyword when this would be ambiguous.

In a similar vein, identifiers with underscores in Agda can be used mixfix, the simplest example of which is probably the if_then_else_ operator:

if_then_else_ : {A : Set} -> Bool -> A -> A -> A
if true  then x else y = x
if false then x else y = y
Jon Purdy
  • 20,437
  • 7
  • 63
  • 95
12

I don't know if you consider MediaWiki wikitext a language, but names with spaces are definitely idiomatic:

==Example==
This example lacks text.
{{Expand section}}

Where "expand section" is the name of a template (http://en.wikipedia.org/wiki/Template:Expand_section)

I guess it meets the criteria - a language where identifiers routinely contain spaces. It's never (I think?) ambiguous because identifiers are always surrounded by lots of punctuation to separate them from raw wiki text.

Steve Bennett
  • 3,419
  • 4
  • 20
  • 24
  • 2
    While wikitext certainly is a formal language, I wouldn't call it a programming language (it doesn't even have loops). – svick Apr 24 '12 at 22:57
  • @svick: Neither do Haskell, Smalltalk, Scheme, Clojure, Erlang, Lambda Calculus, Turing Machines, Io, Ioke, Seph, … – Jörg W Mittag Apr 25 '12 at 11:45
  • @JörgWMittag, but they have recursion, which is just a different way of expressing loops. Wikitext doesn't even have that. – svick Apr 25 '12 at 12:08
  • @svick Depending on which extensions you installed, you get some control structures in mediawiki markup. In particular you get `if`s and recursion. Syntax and performance are pretty bad though. Templates behave pretty much like functions, and their names count as identifiers in my book. – CodesInChaos Apr 25 '12 at 12:38
  • 1
    Interesting, from [[Wikipedia:Transclusion]]: "There is no real looping functionality built into the Mediawiki software as of this time...but there are some tricks for mimicking them. For instance, repeatedly calling a template that repeatedly calls a different template can mimic a double loop. Templates can also be coerced into calling themselves (normally prohibited by the Mediawiki software past a single instance, to prevent infinite loops), by the artful use of redirects (see m:Template:Loop1 (backlinks, edit)) See also m:Help:Recursive conversion of wikitext." – Steve Bennett Apr 26 '12 at 00:46
12

In Algol 68 you could have space in identifiers (I don't remember if they were significant or not). But keywords were marked by stropping. Using names with space in them was idiomatic (at least around me).

VHDL allows escaped identifiers with significant spaces in them: \foo bar\. This allows also to use keywords as identifier \and\, any character \n<42>\ and case sensitivity in identifiers (\Foo\ and \foo\ are different while Foo and foo are equivalent, and different from either \Foo\ and \foo\!). Verilog also has espaced identifiers with most of these characteristics (normal identifiers are case sensitive and escaping them unnecessarily doesn't make another identifier), but doesn't allow spaces in them. The need from escaped identifiers in VHDL and Verilog come from the fact they are often produced automatically from other sources (such as schematic) where identifiers customarily don't have the same restriction as in programming language; AFAIK, they aren't idiomatically used in other circumstances.

AProgrammer
  • 10,404
  • 1
  • 30
  • 45
  • I seem to remember (looking back to the 1980s here!) that CORAL did something similar - you could (and did) have white space in variable names, but keywords then had quotes around them (like `'DEFINE'` and, a personal favourite, `'COMMENT'`. We used to use the macro processor to replace these with un-quoted versions). – AAT Apr 24 '12 at 21:12
11

Well Whitespace is all about... whitespace:

Most modern programming languages do not consider white space characters (spaces, tabs and newlines) syntax, ignoring them, as if they weren't there. We consider this to be a gross injustice to these perfectly friendly members of the character set. Should they be ignored, just because they are invisible? Whitespace is a language that seeks to redress the balance. Any non whitespace characters are ignored; only spaces, tabs and newlines are considered syntax.

Unfortunately Markdown doesn't support its syntax and I can't show you some code, but Wikipedia has a human friendly code sample.

yannis
  • 39,547
  • 40
  • 183
  • 216
8

Scala allows arbitrary identifiers using backticks. The usual use for this is to invoke Thread.`yield` because yield is a reserved word in Scala. This could be (ab)used to have spaces in names, although that would be far from idiomatic Scala code:

val `the answer` = 42
println(`the answer`)

Heck, you can even have tabs in identifiers:

scala> val `the\tanswer` = 42
the     answer: Int = 42

I suppose this could conceivably be idiomatic for the literate programming folk. Maybe.

Peter C
  • 131
  • 3
  • Scala allows characters like `+` in method names. So for `obj.a+=1`, it would parse it as if `a+=` were a method. The inventor Martin Odersky in his textbook assumes that programmers _usually_ include spaces, so that parser ambiguities are practically not too problematic. – Jesvin Jose Apr 25 '12 at 11:20
  • 1
    @aitchnyu: Actually, in mixed identifiers, the alphanumeric part and the operator part need to be separated by an underscore. `obj.a+=1` is equivalent to `obj.a += 1` which is equivalent to `obj.a.+=(1)`. You would need to have `obj.a_+=1` if you want it to work the way you describe. (Actually, that will give a parse error, you need to either call `obj.a_+=(1)` or `obj a_+= 1`.) – Jörg W Mittag Apr 25 '12 at 11:56
  • That's no tab… it's a space station. And by space station, I mean a tab escape sequence. – Thomas Eding Aug 15 '14 at 14:52
5

You might consider this to be the case in Cucumber/Gherkin, where function names are effectively sentences with the arguments embedded inside them.

As an extension, I would expect this to be more common in tiny DSLs, where the language is supposed to be friendly to non developers. For example, many rules engines provide an abilty to define rules with an english-like description, where spaces can be used in identifiers.

Chris Pitman
  • 3,426
  • 1
  • 18
  • 21
5

F# allows white space in identifier names, but they must be surrounded with double backticks. See the answer to this question: https://stackoverflow.com/questions/6639688/using-keywords-as-identifiers-in-f.

Matt H
  • 2,133
  • 16
  • 22
4

FWIW, Tcl allows spaces (and just about every other character) in identifiers, though it's not common to take advantage of this feature. The main reason it's not used very often is just that you have to use proper quoting. For example, the following sets a variable named "my name" to "bob", then prints it

set "my name" "bob"
puts "hello, ${my name}"

OTOH, it's very useful when building variables dynamically since, when creating such variables, one doesn't have to worry about illegal characters

Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
3

If you consider an automated testing DSL a language, the robot framework allows spaces in keyword names, and it's very idiomatic. In the following example "Say hello" is a keyword name, "Example test case" is a test case name, and "${first name}" is a variable:

*** Keywords ***
| Say hello | [Arguments] | ${first name}
| | log | Hello, ${first name}

*** Test Cases ***
| Example test case
| | Say hello | world
Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
3

There are few I know of. I am working on one, and the lithe programming language. Inform does, but it's not exactly a general purpose programming language.

Telastyn
  • 108,850
  • 29
  • 239
  • 365
1

The 4D language allows white space in method names and variables. It is generally frowned upon within the community, but all built-in methods and variables use them when applicable (SET MENU ITEM PARAMETER, for example)

3Doubloons
  • 121
  • 4
1

Powershell allows spaces in variable names:

PS C:\> ${the var} = 100

PS C:\> ${the var}
100
dharmatech
  • 531
  • 5
  • 11
1

I saw mention of similar for VB but in JS this is used a lot actually. Any property of an object in JavaScript can be accessed and set in string form with square brackets or simply as strings in object literals. Property names that don't follow JS's variable naming rules are inaccessible via . notation but they are handy. For instance, you might want to map URLs to behavior or reference a group of people by name when you're certain they're all unique. It's often very convenient and easy to read:

var peoplesFavoriteThings = {
    "Bob Jones":"kittens",
    "Jane Doe":"chainsaws"
}

for(var name in peoplesFavoriteThings){
    console.log(name + ' likes ' + peoplesFavoriteThings[name] + '.\n');
}

This also makes it easy to restructure JSON for ease of use without losing the instant-object factor when dropped into JS.

Erik Reppen
  • 6,243
  • 31
  • 34
  • Funny that this is the only mention of JavaScript. Yes, methods and properties can contain strings: `foo['my method']()` and `foo['my property']` – Steve Bennett Mar 03 '17 at 05:50
0

Smalltalk features keyword methods such as a:b:c: which involve whitespace when invoked. E.g.: a: 100 b: 200 c: 300. This is a standard idiom in the language.

dharmatech
  • 531
  • 5
  • 11
0

Power Query uses a lot of automatically generated code. I'd guess more than half of generated identifiers use white space:

let
    Source = Sql.Database(".", "Test"),
    dbo_pvt = Source{[Schema="dbo",Item="pvt"]}[Data],
    #"Filtered Rows" = Table.SelectRows(dbo_pvt, each [VendorID] <= 4),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Emp1", "Emp2"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Emp3", "Emp4"}, {{"Count", each List.Sum([Emp5]), type number}})
in
    #"Grouped Rows"

As you can see, like in many languages there's extra syntax to disambiguate what the identifier is.

But in places where it's unambiguous, no extra syntax is needed:

let
    spaceRecord = [with space = 42, recursive record = @spaceRecord],
    drilldown = spaceRecord[recursive record][recursive record][recursive record][with space]
in
    drilldown   // 42
Carl Walsh
  • 167
  • 7
-1

If you you count it as a real language, JMP scripting language ( http://www.jmp.com/support/downloads/pdf/jmp_scripting_guide.pdf ). It allows identifiers to have whitespace (to make them more "readable") and more or less encourages such.

Jamie
  • 101
  • 3
-1

The o42a programming language I am currently developing supports a multi-word names. The language has no keywords at all and the names are usually separated with some symbol. In the rare case the two names follow each other, the underscore is used to separate them.

lorus
  • 497
  • 1
  • 3
  • 10
-2

Authorware, whose scripting language was based on non-object-oriented Pascal, allowed spaces in variable names, though over time people discovered the problems with using them and moved away from it http://books.google.com/books?id=bHC88YignAkC&pg=PA428&lpg=PA428 .

Amy Blankenship
  • 805
  • 5
  • 7
-5

Edit: This answer was shown to be not correct, see the comments.

If I understand your question correctly, a compiler can't allow space(s) in identifier name because it could cause duplicate names (unless a delimiter is used). For example:

int my=0; bool my count=false; int count=0; if (my Count)...

the term 'my count' is confusing it could either refer to the variable called 'my count' or maybe the developer forgot to write a relation operator such as > between my and count.

COBOL allowed division names and section names to be separated by space but those are not identifiers and variables as in your question.

NoChance
  • 12,412
  • 1
  • 22
  • 39
  • 5
    Well, it's not the compiler, it's the language definition. Most languages can't allow whitespace in identifiers because they would create ambiguity. – Steve Bennett Apr 24 '12 at 05:15
  • @Steve Bennett, You are correct, thanks for the correction. – NoChance Apr 24 '12 at 05:32
  • 3
    Your reasoning seems kind of iffy to me. In your example the only alternative to `my Count` being a variable name would be the programmer having made a typo. That's not ambiguity. Ambiguity would be if there was another *valid* way to parse the expression. By the same reasoning you could say that allowing `a(b+c)` is ambiguous because maybe the programmer forgot a `>` and really meant `a > (b + c)`. – sepp2k Apr 24 '12 at 05:32
  • @sepp2k, are there any languages that allow a(b+c) instead of a*(b+c)? – NoChance Apr 24 '12 at 05:37
  • 1
    @EmmadKareem I don't know, but in the vast majority of languages `a(b+c)` is syntactically valid and calls the function `a` with the argument `b+c`. – sepp2k Apr 24 '12 at 05:43
  • @sepp2k, you are correct, if you call a function and you use the correct syntax, then there is no ambiguity. To me your example is different than my example. – NoChance Apr 24 '12 at 05:51
  • In this instance "bool my count" might not be exactly ambiguous, but there are very similar expressions that would be parsed differently, like "long int count" (depending on if we're talking about C, C++, javascript...). – Steve Bennett Apr 24 '12 at 05:54
  • 1
    But (in a language that allows spaces in variable names) there is also no ambiguity in `if (my count)`. You're not saying there is a different, valid way to parse that statement (which would mean it's ambiguous). You're saying if you add the character `<`, you end up with a different, valid parse. And I'm saying if you add the character `<` to `a(b+c)` you also end up with a different, valid parse. – sepp2k Apr 24 '12 at 05:55
  • 1
    @SteveBennett Right. Any language that allowed spaces in variable names would either have to disallow them in type names or use a different syntax for type declarations (like say `var name of the variable : type of the variable`) - or not have type declarations at all. – sepp2k Apr 24 '12 at 05:56
  • 1
    @sepp2k, now I got your point. Thanks for taking the time to make it clear. My answer is incorrect. – NoChance Apr 24 '12 at 05:57
  • @sep2k: there are other possibilities. A language could have a way of disambiguating (eg, "foo bar baz" checks first for a type "foo bar", then for a type "foo", and falls back to having a typeless variable "foo bar baz"). It would be a pain in the arse to work with, of course. – Steve Bennett Apr 24 '12 at 06:29