Use of special characters in function names
Asked Answered
V

4

28

In Ruby, a standard convention is to use a question mark at the end of a method name to indicate the method returns a boolean result:

[].empty?   #=> true 

Another standard convention is to end a method name with an exclamation point if the method is destructive (that is, it modifies the original data):

mylist.sort! # sort mylist in-place

Recently I have seen these same conventions used in Scheme. Which makes me wonder, what other languages use/support this convention? Are there any other special characters that are commonly used for naming by these or other languages?

Ventris answered 21/10, 2009 at 2:18 Comment(0)
U
20

The answer is, of course, language (and language culture) specific.

For example, depending on the language, all of the following are appropriate: empty-p, empty?, empty, is_empty or isEmpty. (These examples are, of course, not inclusive).

The examples in the original question come from Ruby where such use of ? and ! to end method names are, where appropriate, accepted. This acceptance comes from 1) readily accessible as symbol terminators in the grammar 2) use of ? and ! in the standard library. However, it should be note that ! is not used universally to imply "side-effect" and is generally only present on alternative forms: save/save!, sort/sort!, etc. There are an overwhelming number of methods that perform side-effects which do not have the ! prefix.

Personally, if I was designing a language, I would allow ?, ! and ' to be valid unquoted trailing characters in symbol names. Even though that some languages allow full symbol escaping, such as Scala, such symbols are usually avoided because it's a pain to have to quote them for use.

// in Scala, esp. with Java-compat, the last form is generally used although
// the first two are perfectly valid -- do what makes sense in the language
foo.`empty?`
foo.empty_?
foo.isEmpty

When in Rome...

  • Scala - empty? or empty_? (not common)
  • C/C++/JS/Java/Python/Perl - no ? or ! in identifiers; JS/Java allow $; $ is a sigil in Perl
  • C/C++/Perl/JS - hands up in the air?
  • C# - IsEmpty (method or property) or Empty (property)
  • Python - is_empty or isEmpty per Guido-guide (although usually len protocol: if not len(foo): "empty!")
  • Java - isEmpty per Language Guide
  • Ruby - special trailing ? and ! (quoting?)
  • JS - indirect identifier access: obj["empty?"] (not common for this)
  • Lisp (conventional): empty-p for predicate; variations in reader/quoting
  • Julia - appends ! to functions that modify their arguments

Glad to see corrections and/or additions -- is only a drop in a bucket.

Uxorious answered 21/10, 2009 at 2:18 Comment(4)
Help help! How can I turn the bottom part of this post into a "community wiki"?Favianus
Anyone can edit your post, so it is a community wiki. What are you trying to do? I can incorporate these and everyone else's comments into the original top-level question so there is a single list for everyone.Ventris
Preferably just to have the list at the bottom edited. I wasn't sure that it could be edited by others. This stackoverflow thing is still a bit new to me.Favianus
there has been a long history in lisp using question marks: (if empty? ...).Xenolith
W
3

I'm not a big fan of special characters in function names, but then I'll also beat to death, with a stick of wet celery to tease out the pain, anyone I find putting spaces into file names :-)

The ? variant can just as easily be done with something like isEmpty() and the ! variant with sortInPlace().

English is a very adaptive language, even without the punctuation.

In either case, my languages of choice (C and Java) use punctuation for all sorts of other things. Having them in identifiers as well would make the lexical analysis a nightmare.

Warrantee answered 21/10, 2009 at 2:18 Comment(2)
"Having them in identifiers as well would make the lexical analysis a nightmare." This statement is simply not true. While I will not defend Ruby's syntax (which is not context free and has many odd quirks: e.g. <code>a??b:c</code>), the use of non [a-z_][a-z0-9_] is not necessarily more complicated to run through a lexer. Consider Scheme as a very "simple" counter-example (and Perl as, perhaps a prime example, although it does NOT allow "special" characters in identifiers). The use of such symbols, however, is wide open to opinion.Favianus
@Favianus Ruby has a more uniform grammar. I think it is not context sensitive and can be described in BNF. C++ requires context to parse and cannot be described in BNF. Adding special characters to identifiers would complicate parsing, but I agree that in principle parsing would still be possible, perhaps even swiftly, but I think the code to do that would difficult in practice.Frech
L
1

Prepending "@" to a method name (or any identifier) in C# allows you to name that method with a normally-reserved keyword.

void null() {}
// Invalid token 'null' in class, struct, or interface member declaration

void @null()
// compiles

Don't do this; it's mainly for machine-generated code, like datasets. MSDN page on the topic.

Linda answered 21/10, 2009 at 2:18 Comment(1)
Is there any way to quote an arbitrary identifier in C#?Favianus
M
-1

If you count Javascript as a language, many frameworks seem to make use of the '$' character to mean "Selector"; For example, one might use $("id-of-some-item") in Prototype.js or $("#id-of-some-item") in jQuery to select a tag that was written <div id="id-of-some-item">. The use of these selection functions is actually quite complicated and powerful (if you're interested, check out their supporting documentation at prototypejs.org and jquery.com), but they boil down to doing something I consider semantically similar to what you've indicated.

Again, this is assuming you count Javascript as a language.

Milligram answered 21/10, 2009 at 2:18 Comment(6)
If? That's a bit harsh, isn't it?Warrantee
Oh, let me pull out the tape ... it's even Turing Complete. (Geek test.) Yes, it is a computer programming language, perhaps not liked by some, but Lemon Meringue Pie doesn't suddenly stop becoming pie because I detest how it tastes.Favianus
I would not count on this ($ == Selector): #206353Beckman
Juri, I was only commenting on the apparent convention created by popular frameworks, there is no more a rule in JavaScript for these characters than in Ruby for ? and !, or in Haskell for ' -- it's just convention Paxdiablo, I am simply trying to appease the crowd who gets all curmudgeon-y if you imply that a weak/dynamically-typed interpreted-language counts as programming. Of course it does, and getting it right in a major application can be every bit as difficult as accomplishing other goals with other tools; it's all about picking the right one for the job.Milligram
if you imply that a weak/dynamically-typed interpreted-language counts as programming wtf? hope you changed your mind by nowSabba
I'm a web developer; I've been a web developer for 10 years. I think that web development is a complicated animal with many wonderful, powerful tools in its toolbelt. I suppose you missed the very next sentence, which began with "Of course it does"? Or even the rest of the sentence which that very quote came from?Milligram

© 2022 - 2024 — McMap. All rights reserved.