Reserved words as names or identifiers
No, there is no way. That's why they're labeled "reserved".
What is the difference between keyword and reserved word?
Keywords have a special meaning in a language, and are part of the syntax.
Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language.
In practice most keywords are reserved words and vice versa. But because they're two different things it may happen that a keyword is not a reserved word (e.g. a keyword only has meaning in a special context, and can therefore be used as an identifier), or a reserved word is not a keyword (e.g. because it is reserved for future use).
Update: Some examples as given by others that illustrate the distinction:
- In Java,
goto
is a reserved word but not a keyword (as a consequence, you cannot use it at all) - Fortran has no reserved words, all keywords (if, then, etc.) can be used as identifiers
Classes with reserved names (keywords). How do you deal with this?
First off, this depends a lot of the language.
TALK TO YOUR TEAM
Secondly, and most important! Conventions may change a lot depending on your team. Talk to your team, and agree on one convention to use. Never forget this.
Conventions
There are universal conventions/standards to follow, and like you mentioned, using keywords is usually a bad-ish idea. I for one try to avoid them, just as I avoid using digits in my variable-names, even if the specific language I'm working with allows it. Reason? It's easier to stick with "let's avoid problems by mixing rules between languages" than having to check the rules each time.
I often spend 30 minutes thinking of the perfect variable name, so I am quite used to this kind of pondering.
Length
Excessively long variable names is bad, because it hinders flow reading, while excessively short variable names are also bad because it is hard to guess what word you want. You could call it srvc
, sure, but who will know what that means in a month (unless you comment it, sure). Dropping the vowels in user-variables is quite common, actually, especially in low-level/old languages.
Specific case
As for this specific example, I wouldn't think of MedicalService
as a keyword. First off, it's part of a longer name, like MedicalFile
doesn't look like a file from the system at all, but rather a form with medical data on it.
I don't exactly know what this MedicalService
does, but it seems like a generic (abstract, probably) class name for services that you can ask for at the counter of a hospital, so I'm assuming that.
GenericMedicalThingToDo
is a funny way to avoid the keyword, but I wouldn't call it that. MedicalUseCase
seems quite better, and gets to the point.
On the other hand, if this is just a string stating the use case for whatever it is the user has chosen (considering you mentioned Angular), I would just stick with userMedicalChoice
(drop the PascalCase to camelCase).
If you need to use a word that is actually a keyword, which often happens, you might want to add a _
on the end or the beginning of it. This is not usually good for interfaces, as it's conventional to only use those internally/privately. Some conventions use double _
for private, and single _
to avoid dupes.
Last point:
Having keywords as part of a longer variable name is not a problem in any of the many programming languages I have sailed in, so just call it MedicalService
, or GenericMedicalService
if you're going to subclass it.
PS: Read up on some conventions of different languages, like PEP-8, and PEP-256 from Python, or Google's C++ conventions. While not specifically being valid for all languages, they do give you something pin-pointers to what is important.
Do reserved words need to be quoted when set as property names of JavaScript objects?
ECMAScript 5+
No, quotes were not needed since ECMAScript 5. Here's why:
As mentioned in your post, from the ECMAScript® 5.1 Language Specification:
7.6 Identifier Names and Identifiers
Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An
Identifier
is anIdentifierName
that is not aReservedWord
(see 7.6.1).[...]
Syntax
Identifier ::
IdentifierName but not ReservedWord
By specification, a ReservedWord
is:
7.6.1 Reserved Words
A reserved word is an
IdentifierName
that cannot be used as anIdentifier
.Syntax
ReservedWord ::
Keyword
FutureReservedWord
NullLiteral
BooleanLiteral
This includes keywords, future keywords, null
, and boolean literals. The full list is as follows:
7.6.1.1 Keywords
break do instanceof typeof
case else new var
catch finally return void
continue for switch while
debugger function this with
default if throw
delete in try
7.6.1.2 Future Reserved Words
class enum extends super
const export import
7.8.1 Null Literals
null
7.8.2 Boolean Literals
true
false
The above (Section 7.6) implies that IdentifierName
s can be ReservedWord
s, and from the specification for object initializers:
11.1.5 Object Initialiser
[...]
Syntax
ObjectLiteral :
{ }
{ PropertyNameAndValueList }
{ PropertyNameAndValueList , }
Where PropertyName
is, by specification:
PropertyName :
IdentifierName
StringLiteral
NumericLiteral
As you can see, a PropertyName
may be an IdentifierName
, thus allowing ReservedWord
s to be PropertyName
s. That conclusively tells us that, by specification, it is allowed to have ReservedWord
s such as class
and var
as PropertyName
s unquoted just like string literals or numeric literals.
ECMAScript <5
To go more in depth as to why this wasn't allowed in previous versions before ES5, you have to look at how PropertyName
was defined. Per the ECMAScript® 3 Language Specification:
PropertyName :
Identifier
StringLiteral
NumericLiteral
As you can see, PropertyName
was an Identifer
- not an IdentifierName
, thus leading to the inability for ReservedWord
s as PropertyName
s.
Is it possible to use reserved words for field names?
Try using & before the field name
Can Java class files use reserved keywords as names?
The only restrictions on class names at the bytecode level are that they can't contain the characters [
, .
or ;
and that they're at most 65535 bytes long. Among other things, this means that you can freely use reserved words, whitespace, special characters, Unicode, or even weird stuff like newlines.
You can theoretically even use null characters in a class name, but since it's impossible to have a null character in the filename, you can't include such a classfile in a jar. You might be able to create and load one dynamically though.
Here's an example of some of the things that you can do (written in Krakatau assembly):
; Entry point for the jar
.class Main
.super java/lang/Object
.method public static main : ([Ljava/lang/String;)V
.limit stack 10
.limit locals 10
invokestatic int hello ()V
invokestatic "-42" hello ()V
invokestatic "" hello ()V
invokestatic " some whitespace and \t tabs" hello ()V
invokestatic "new\nline" hello ()V
invokestatic 'name with "Quotes" in it' hello ()V
return
.end method
.end class
.class int
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from int"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class "-42"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from -42"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
; Even the empty string can be a class name!
.class ""
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from "
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class " some whitespace and \t tabs"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from some whitespace and \t tabs"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class "new\nline"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from new\nline"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class 'name with "Quotes" in it'
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from name with \"Quotes\" in it"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
Execution output:
Hello from int
Hello from -42
Hello from
Hello from some whitespace and tabs
Hello from new
line
Hello from name with "Quotes" in it
See Holger's answer for the exact quote of the rules from the JVM specification.
Related Topics
What's in an Eclipse .Classpath/.Project File
How to Change Timezone for a Java.Util.Calendar/Date
Converting an Int to a Binary String Representation in Java
Uses for the Java Void Reference Type
Java Regex for Support Unicode
Is There Something Like Annotation Inheritance in Java
Java.Lang.Outofmemoryerror: Permgen Space
Programmatically Change Log Level in Log4J2
Filtering Database Rows with Spring-Data-JPA and Spring-Mvc
Sorting Strings That Contains Number in Java
How to Call a Method in Another Class of the Same Package
Printing My MAC's Serial Number in Java Using Unix Commands
Java Sax Parser Split Calls to Characters()
Exception Starting Filter Struts2 - Tried Adding Jar'S, But Same Result