Wednesday, February 23, 2005

Keywords are not commands

Introduction

Generally, keywords are not commands, operators, methods or something that does anything useful — well, at least most of the time, with the exception of the instanceof operator.

Visually, we use a space after the keyword token to indicate that it is not a method, although the space is not required by the syntax.

Code Convention: Always space after keyword
if (condition) { // don't write: if(condition) {
    statements;
}
Code Convention: Never space after method identifier
System.exit(0); // don't write: System.exit (0);

Discussion

Keywords are reserved because they are part of the language syntax, making the syntax easier for programmers to read, not because they have any meaning. (With the exception of languages such as PostScript, where keywords are lists of rules.)

For example, Dahl and Nygaard (SIMULA 1965) say that the symbol new is redundant. If its redundant, then obviously it can't be a command. Dahl and Nygaard (SIMULA 1965) also say that the symbol new often can be used to advantage to improve readability.

Concept: The real function of keywords is to make it look more like english, making it easier for humans to remember the syntax and to read a program. You should always consider the whole grammar rule that the keyword is a part of. Do not assume that a keyword should do anything by itself. If you do think really hard about what a keyword should be expected to do, you will most likely just end up confused.

keywords as operators

In some languages like Simula 67, a keyword could also be an operator, which is a bad idea because keywords as operators complicates the language. For example, the operator > could be represented as the keyword gt. Probably they used keyboards without that character (1). In Java, most keywords are not operators, except for instanceof.

Conclusion

Keywords are not commands. Keywords are part of grammar rules, making programs easier to read and understand for humans. You may convince yourself by examining The Java JLS3 Grammar.

References

1. If you have an reference for this one, please tell me!

Dahl and Nygaard, 1965, SIMULA, If you have an reference for this one, please tell me!

No comments: