[ACCEPTED]-Use of special characters in function names-method-names
The answer is, of course, language (and 24 language culture) specific.
For example, depending 23 on the language, all of the following are 22 appropriate: empty-p, empty?, empty, is_empty 21 or isEmpty. (These examples are, of course, not 20 inclusive).
The examples in the original 19 question come from Ruby where such use of 18 ? and ! to end method names are, where appropriate, accepted. This 17 acceptance comes from 1) readily accessible 16 as symbol terminators in the grammar 2) use 15 of ? and ! in the standard library. However, it 14 should be note that ! is not used universally 13 to imply "side-effect" and is 12 generally only present on alternative forms: save/save!, sort/sort!, etc. There 11 are an overwhelming number of methods that 10 perform side-effects which do not have the 9 ! prefix.
Personally, if I was designing 8 a language, I would allow ?, ! and ' to 7 be valid unquoted trailing characters in 6 symbol names. Even though that some languages 5 allow full symbol escaping, such as Scala, such 4 symbols are usually avoided because it's 3 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 2 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 1 additions -- is only a drop in a bucket.
I'm not a big fan of special characters 11 in function names, but then I'll also beat 10 to death, with a stick of wet celery to 9 tease out the pain, anyone I find putting 8 spaces into file names :-)
The ?
variant can 7 just as easily be done with something like 6 isEmpty()
and the !
variant with sortInPlace()
.
English is a very 5 adaptive language, even without the punctuation.
In 4 either case, my languages of choice (C and 3 Java) use punctuation for all sorts of other 2 things. Having them in identifiers as well 1 would make the lexical analysis a nightmare.
Prepending "@" to a method name 4 (or any identifier) in C# allows you to 3 name that method with a normally-reserved 2 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 1 code, like datasets. MSDN page on the topic.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.