6) What characters are allowed in a C function name identifier.? Explanation: Remember that a C function name can not start with a number but it can contain contain numbers after 1st character is either an Underscore ( _ ) or an Alphabet.
Python3. An identifier in Python cannot use any special symbols like !, @, #, $, % etc.
The period, the underscore, and the characters $, #, and @ can be used within variable names. For example, A. _$@#1 is a valid variable name. Variable names ending with a period should be avoided, since the period may be interpreted as a command terminator.
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...
empty?
or empty_? (not common)if not len(foo): "empty!"
)Glad to see corrections and/or additions -- is only a drop in a bucket.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With